traverse array of objects javascript code example
Example 1: javascript loop through array of objects
let arr = [object0, object1, object2];
for (let elm of arr) {
console.log(elm);
}
Example 2: javascript loop through array of objects
var people=[
{first_name:"john",last_name:"doe"},
{first_name:"mary",last_name:"beth"}
];
for (let i = 0; i < people.length; i++) {
console.log(people[i].first_name);
}
Example 3: ho to loop trough an array of objects
yourArray.forEach(function (arrayItem) {
var x = arrayItem.prop1 + 2;
console.log(x);
});
Example 4: iterate over array of objects javascript
const myArray = [{x:100}, {x:200}, {x:300}];
const newArray= myArray.map(element => element.x);
console.log(newArray);
Example 5: iterate over array of objects javascript
const people = [
{name: 'John', age: 23},
{name: 'Andrew', age: 3},
{name: 'Peter', age: 8},
{name: 'Hanna', age: 14},
{name: 'Adam', age: 37}];
const anyAdult = people.some(person => person.age >= 18);
console.log(anyAdult);
Example 6: iterate over array of objects javascript
const myArray = [{x:100}, {x:200}, {x:300}];
const newArray = myArray.filter(element => element.x > 250);
console.log(newArray);