In: Computer Science
JavaScript
Given the following object, log every property name and value to the console using a loop.
let myObj = { id: 12 name: 'My Object', class: 'obj', height: 65, likeJavascript: true, data: [1, 53, 23] };
Find the code for the above question below, read the
comments provided in the code for better understanding. If found
helpful please do upvote this.
Please refer to the screenshot of the code to understand the
indentation of the code.
To loop a objects use the Object.keys() method , it returns all the keys in an object which can be used to iterate
Code
//Given object
let myObj = {
id: 12,
name: 'My Object',
class: 'obj',
height: 65,
likeJavascript: true,
data: [1, 53, 23]
};
//use the OBject.keys() methods
Object.keys(myObj).forEach(a=>{
//log the key and value
console.log("Property Name : ",a," , Value : ",myObj[a])
})
Screenshot
Output