how to use foreach loop with object code example
Example 1: foreach object javascript
const students = {
adam: {age: 20},
kevin: {age: 22},
};
Object.entries(students).forEach(student => {
console.log(`Student: ${student[0]} is ${student[1].age} years old`);
});
Example 2: for each element in object
var obj = {
first: "John",
last: "Doe"
};
Object.keys(obj).forEach(function(key) {
console.log(key, obj[key]);
});
Example 3: foreach object javascript
const obj = {
a: "aa",
b: "bb",
c: "cc",
};
for(let key in obj) {
console.log(key);
console.log(obj[key]);
}