js loop through array of objects 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
let arr = [object0, object1, object2];
for (let elm of arr) {
console.log(elm);
}
Example 3: 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 4: js loop over array of objects extract value
var myArr = [{name: 'rich', secondName: 'james'}, {name: 'brian', secondName: 'chris'}];
var mySecondArr = myArr.map(x => x.name);
console.log(mySecondArr);
Example 5: js loop array of objects
const p = [{
"p1": "value1",
"p2": "value2",
"p3": "value3"
},
{
"p4": "value4",
"p5": "value5",
"p6": "value6"
}];
for (let obj of p) {
for(let key in obj) {
console.log(key);
}
}
Example 6: 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);
});