Answers for "js iterating over array of objects"

8

javascript loop through array of objects es6

/* new options with IE6: loop through array of objects */

const people = [
  {id: 100, name: 'Vikash'},
  {id: 101, name: 'Sugam'},
  {id: 102, name: 'Ashish'}
];

// using for of
for (let persone of people) {
  console.log(persone.id + ': ' + persone.name);
}

// using forEach(...)
people.forEach(person => {
 console.log(persone.id + ': ' + persone.name);
});
// output of above two methods
// 100: Vikash
// 101: Sugam
// 102: Ashish


// forEach(...) with index
people.forEach((person, index) => {
 console.log(index + ': ' + persone.name);
});
// output of above code in console
// 0: Vikash
// 1: Sugam
// 2: Ashish
Posted by: Guest on May-04-2020
0

iterate over array of objects javascript

// Transform to a new array

const myArray = [{x:100}, {x:200}, {x:300}];

const newArray= myArray.map(element => element.x);
console.log(newArray); // [100, 200, 300]
Posted by: Guest on January-02-2021

Code answers related to "js iterating over array of objects"

Code answers related to "Javascript"

Browse Popular Code Answers by Language