javascript foreach example
/* 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"const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
console.log(index, item)
})/** 1. Use forEach and related */
var a = ["a", "b", "c"];
a.forEach(function(entry) {
console.log(entry);
});
/** 2. Use a simple for loop */
var index;
var a = ["a", "b", "c"];
for (index = 0; index < a.length; ++index) {
console.log(a[index]);
}
/**3. Use for-in correctly*/
// `a` is a sparse array
var key;
var a = [];
a[0] = "a";
a[10] = "b";
a[10000] = "c";
for (key in a) {
if (a.hasOwnProperty(key) && // These checks are
/^0$|^[1-9]\d*$/.test(key) && // explained
key <= 4294967294 // below
) {
console.log(a[key]);
}
}
/** 4. Use for-of (use an iterator implicitly) (ES2015+) */
const a = ["a", "b", "c"];
for (const val of a) {
console.log(val);
}
/** 5. Use an iterator explicitly (ES2015+) */
const a = ["a", "b", "c"];
const it = a.values();
let entry;
while (!(entry = it.next()).done) {
console.log(entry.value);
}
let words = ['one', 'two', 'three', 'four'];
words.forEach((word) => {
console.log(word);
});
// one
// two
// three
// fourvar colors = ["red", "blue", "green"];
colors.forEach(function(color) {
console.log(color);
});var items = ["item1", "item2", "item3"]
var copie = [];
items.forEach(function(item){
copie.push(item);
});