Answers for "how to iterate through an object javascript"

85

javascript loop through object

Object.entries(obj).forEach(
    ([key, value]) => console.log(key, value)
);
Posted by: Guest on February-15-2020
11

javascript loop through object

// object to loop through
let obj = { first: "John", last: "Doe" };

// loop through object and log each key and value pair
//ECMAScript 5
Object.keys(obj).forEach(function(key) {
    console.log(key, obj[key]);
});

//ECMAScript 6
for (const key of Object.keys(obj)) {
    console.log(key, obj[key]);
}

//ECMAScript 8
Object.entries(obj).forEach(
    ([key, value]) => console.log(key, value)
);

// OUTPUT
/*
   first John
   last Doe
*/
Posted by: Guest on May-12-2020
13

javascript loop through object

for (var property in object) {
  if (object.hasOwnProperty(property)) {
    // Do things here
  }
}
Posted by: Guest on September-09-2019
-1

js loop through object

const obj = { a: 1, b: 2, c: 3 }

for (const [key, value] of Object.entries(obj)) {
	console.log(key, value)
}
Posted by: Guest on January-08-2021
0

javascript looping through object

const fruits = {
  apple: 28,
  orange: 17,
  pear: 54,
}

const entries = Object.entries(fruits)
console.log(entries)
// [
//   [apple, 28],
//   [orange, 17],
//   [pear, 54]
// ]
Posted by: Guest on May-19-2020

Code answers related to "how to iterate through an object javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language