object for each in javascript 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 iterate over object
var obj = { first: "John", last: "Doe" };
Object.keys(obj).forEach(function(key) {
console.log(key, obj[key]);
});
Example 3: for each of object
Object.keys(object).map(function(objectKey, index) {
var value = object[objectKey];
console.log(value);
});
Example 4: javascript foreach object
function logArrayElements(element, index, array) {
console.log('a[' + index + '] = ' + element)
}
[2, 5, , 9].forEach(logArrayElements)