Last active
October 22, 2015 06:30
-
-
Save ph4un00b/fdc93183da6736f9c5cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title></title> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
var jamones = [ | |
{ | |
"id": 3, | |
"title": "serrano", | |
"location": "df", | |
"image": "http://www.hamazing.com/image/cache/data/jamon/serrano/Teruel-Jamon-Serrano-IGP-Organic-Free-Range-712x400.jpg", | |
}, | |
{ | |
"id": 4, | |
"title": "pavo", | |
"location": "monterrey", | |
"image": "http://braedt-catalogo.com.pe/uploads/b/Jamyn-de-Pechuga-de-Pavo-Ahumado-.jpg", | |
}, | |
{ | |
"id": 5, | |
"title": "york", | |
"location": "guadalajara", | |
"image": "http://www.abc.es/Media/201401/24/jamon-york--478x270.jpg", | |
}, | |
{ | |
"id": 6, | |
"title": "americano", | |
"location": "puebla", | |
"image": "http://braedt-catalogo.com.pe/uploads/b/jamon-americano.jpg", | |
} | |
]; | |
console.log('===== FOR LOOP show title'); | |
for(var counter=0; counter < jamones.length; counter++){ | |
console.log(jamones[counter].title); | |
} | |
console.log('===== FOREACH show title'); | |
jamones.forEach(function(jamon) { | |
console.log(jamon.title); | |
}) | |
// este metodo va aplicar una function a cada elemento de la collection | |
// y regresa un nuevo arreglo | |
Array.prototype.fMap = function (callback) { | |
var collection = []; | |
this.forEach(function(item) { | |
collection.push( callback(item) ); | |
}); | |
return collection; | |
} | |
console.log('===== MAP show title'); | |
jamones.fMap(function(jamon) { | |
console.log(jamon.title); | |
}); | |
var lista = jamones.forEach(function(pasto){ | |
return pasto.nuevo_attributo = 'for each'; | |
}); | |
console.log('===== FOREACH nuevo attributo'); | |
console.log(lista); | |
console.log(jamones); | |
var jamones = [ | |
{ | |
"id": 3, | |
"title": "serrano", | |
"location": "df", | |
"image": "http://www.hamazing.com/image/cache/data/jamon/serrano/Teruel-Jamon-Serrano-IGP-Organic-Free-Range-712x400.jpg", | |
}, | |
{ | |
"id": 4, | |
"title": "pavo", | |
"location": "monterrey", | |
"image": "http://braedt-catalogo.com.pe/uploads/b/Jamyn-de-Pechuga-de-Pavo-Ahumado-.jpg", | |
}, | |
{ | |
"id": 5, | |
"title": "york", | |
"location": "guadalajara", | |
"image": "http://www.abc.es/Media/201401/24/jamon-york--478x270.jpg", | |
}, | |
{ | |
"id": 6, | |
"title": "americano", | |
"location": "puebla", | |
"image": "http://braedt-catalogo.com.pe/uploads/b/jamon-americano.jpg", | |
} | |
]; | |
var lista = jamones.fMap(function(jamon) { | |
return { nuevo_attributo: 'map', title: jamon.title }; | |
}); | |
console.log('===== MAP nuevo attributo'); | |
console.log(lista); | |
console.log(jamones); | |
console.log('===== FOR LOOP new title'); | |
for(var counter=0; counter < jamones.length; counter++){ | |
jamones[counter].title = 'new title'; | |
} | |
console.log(jamones); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment