Answers for "how add for loop on array of object in javascript"

1

js loop array of objects

// Array of objects
const p = [{
  "p1": "value1",
  "p2": "value2",
  "p3": "value3"
},
{
  "p4": "value4",
  "p5": "value5",
  "p6": "value6"
}];

// Get the objects out of the array
for (let obj of p) {
  // console.log(obj);
  // output: 
  // { p1: 'value1', p2: 'value2', p3: 'value3' }
  // { p4: 'value4', p5: 'value5', p6: 'value6' }
  // Now we can loop the objects in the array by nesting the 'for in' loop inside the 'for of' loop
  for(let key in obj) {
    console.log(key);
    // output:
    // p1
    // p2
    // p3
    // p4
    // p5
    // p6
    // console.log(obj[key]);
    // output
    // value1
    // value2
    // value3
    // value4
    // value5
    // value6
  }
}
Posted by: Guest on April-02-2021
0

javascript for loop array of objects

var array = ["e", 5, "cool", 100];

for (let i = 0; i < array.length; i++) {
	console.log(array[i]);
}

// This is a common method used to loop through elements in arrays.
// You can use this to change elements, read them, and edit them
Posted by: Guest on May-27-2021

Code answers related to "how add for loop on array of object in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language