forEach on a javascript 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: javascript loop through object
for (var property in object) {
if (object.hasOwnProperty(property)) {
}
}
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]);
}
Example 4: for each of object
Object.keys(object).map(function(objectKey, index) {
var value = object[objectKey];
console.log(value);
});