Answers for "nodejs how to loop object"

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
2

how to loop object javascript

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

for (const prop in obj) {
  console.log(`obj.${prop} = ${obj[prop]}`);
}

// Salida:
// "obj.a = 1"
// "obj.b = 2"
// "obj.c = 3"
Posted by: Guest on May-15-2020
0

loop through an object

Object.keys
Object.values
Object.entries

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 April-25-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language