Answers for "how to loop through all items in object and print key and value"

6

javascript loop object

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

Object.keys(obj).forEach(key => {
    console.log(key, obj[key]);
});
// key1 value1
// key2 value2
// key3 value3

// using for in - same output as above
for (let key in obj) {
  let value = obj[key];
  console.log(key, value);
}
Posted by: Guest on May-04-2020
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 loop through all items in object and print key and value"

Code answers related to "Javascript"

Browse Popular Code Answers by Language