each javascript
/* In the following example, colors array has 3 items. Let’s use forEach() to
log to console every color: */
const colors = ['blue', 'green', 'white'];
function iterate(item) {
console.log(item);
}
colors.forEach(iterate);
// logs "blue"
// logs "green"
// logs "white"var arr = ['one','two','three','four','five'];
$.each(arr, function(index, value){
console.log('The value at arr[' + index + '] is: ' + value);
});const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
console.log(index, item)
})$( "li" ).each(function( index ) {
console.log( index + ": " + $( this ).text() );
});const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
// Iterate over fruits below
// Normal way
fruits.forEach(function(fruit){
console.log('I want to eat a ' + fruit)
});function each(collection, action) {
if (Array.isArray(collection)) {
for (var i = 0; i < collection.length; i++) {
action(collection[i], i, collection);
}
} else {
for (var key in collection) {
action(collection[key], key, collection);
}
}
}