for loop for array object in 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 object array iteration

let obj = {
  key1: "value1",
  key2: "value2",
  key3: "value3"
}

Object.keys(obj).forEach(key => {
  let value = obj[key];
  //use key and value here
});

Example 3: how to get value in array object value using for loop in javascript

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

const newArray= myArray.map(element => element.x);
console.log(newArray); // [100, 200, 300]

Example 4: javascript loop through array of objects

var arr = [{id: 1},{id: 2},{id: 3}];

for (var elm of arr) {
  console.log(elm);
}

Example 5: iterate over array of object javascript and access the properties

for (let item of items) {
    console.log(item); // Will display contents of the object inside the array
}