for each for array in javascript code example
Example 1: for each js
const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
fruits.forEach(function(fruit){
console.log('I want to eat a ' + fruit)
});
Example 2: For-each over an array in JavaScript
var a = ["a", "b", "c"];
a.forEach(function(entry) {
console.log(entry);
});
var index;
var a = ["a", "b", "c"];
for (index = 0; index < a.length; ++index) {
console.log(a[index]);
}
var key;
var a = [];
a[0] = "a";
a[10] = "b";
a[10000] = "c";
for (key in a) {
if (a.hasOwnProperty(key) &&
/^0$|^[1-9]\d*$/.test(key) &&
key <= 4294967294
) {
console.log(a[key]);
}
}
const a = ["a", "b", "c"];
for (const val of a) {
console.log(val);
}
const a = ["a", "b", "c"];
const it = a.values();
let entry;
while (!(entry = it.next()).done) {
console.log(entry.value);
}
Example 3: js for each item do
let array = ['Item 1', 'Item 2', 'Item 3'];
array.forEach(item => {
console.log(item);
});
Example 4: how to use the foreach method in javascript
groceries.forEach(groceryItem => console.log(groceryItem));