es6 loop through array 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: js loop through array
for (const color of colors){
console.log(color);
}
const array = ["one", "two", "three"]
array.forEach(function (item, index) {
console.log(item, index);
});
for (var i = 0; i < arrayLength; i++) {
console.log(myStringArray[i]);
}
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);
});
Example 4: javascript iterate array
String[] myStringArray = {"Hello", "World"};
for (String s : myStringArray)
{
}