javascript 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: loop through arrays in es6
var sandwiches = [
'tuna',
'ham',
'turkey',
'pb&j'
];
sandwiches.forEach(function (sandwich, index) {
console.log(index);
console.log(sandwich);
});