js for loop for object code example

Example 1: javascript loop through object

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

Example 2: javascript loop through object

var obj = { first: "John", last: "Doe" };

Object.keys(obj).forEach(function(key) {
    console.log(key, obj[key]);
});

Example 3: loop key in object

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

for(key in fruits){
	console.log(key)
}

Example 4: for in loop javascript

var person = {"name":"taylor","age":31};
for (property in person) {
	console.log(property,person[property]);
}
//name taylor
//age 31

Example 5: javascript loop through object

var person = {"name":"bob", age:45};

for (var property in person) {
   console.log(person[property]);
}