looping over array of objects javascript code example
Example 1: javascript loop through object example
var person={
first_name:"johnny",
last_name: "johnson",
phone:"703-3424-1111"
};
for (var property in person) {
console.log(property,":",person[property]);
}
Example 2: 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 3: loop array of objects
const myArray = [{x:100}, {x:200}, {x:300}];
myArray.forEach((element, index, array) => {
console.log(element.x);
console.log(index);
console.log(array);
});
Example 4: how to loop through an array of objects+
var arr = [[1,2], [3,4], [5,6]];
for (var i=0; i < arr.length; i++) {
for (var j=0; j < arr[i].length; j++) {
console.log(arr[i][j]);
}
}
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);