Answers for "loop through keys in javascript"

12

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
0

going through every attributes of an object javascript

let a = {x: 200, y: 1}
let attributes = Object.keys(a)
console.log(attributes)
//output: ["x", "y"]
Posted by: Guest on April-09-2020

Code answers related to "loop through keys in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language