es6 for each code example
Example 1: javascript loop through array of objects es6
const people = [
{id: 100, name: 'Vikash'},
{id: 101, name: 'Sugam'},
{id: 102, name: 'Ashish'}
];
for (let persone of people) {
console.log(persone.id + ': ' + persone.name);
}
people.forEach(person => {
console.log(persone.id + ': ' + persone.name);
});
people.forEach((person, index) => {
console.log(index + ': ' + persone.name);
});
Example 2: es6 forEach
const array1 = ['a', 'b', 'c'];
array1.forEach((element) => {
console.log(element)
});
Example 3: foreach jas
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
Example 4: javascript foreach syntax
const marks = [12, 17, 14];
let sum = 0;
marks.forEach(mark => {
sum += mark;
});