js foreach on 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: javascript foreach object
const list = {
key: "value",
name: "lauren",
email: "[email protected]",
age: 30
};
Object.keys(list).forEach(val => {
let key = val;
let value = list[val];
console.log(`${key} : ${value}`);
});
Example 4: js loop through object
const obj = { a: 1, b: 2 };
Object.keys(obj).forEach(key => {
console.log("key: ", key);
console.log("Value: ", obj[key]);
} );
Example 5: object foreach
const obj = {
name: 'Jean-Luc Picard',
rank: 'Captain'
};
Object.keys(obj).forEach(key => {
console.log(key, obj[key]);
});
Example 6: foreach object javascript
const obj = {
name: 'Jean-Luc Picard',
rank: 'Captain'
};
Object.entries(obj).forEach(entry => {
const [key, value] = entry;
console.log(key, value);
});