foreach for array in object code example
Example 1: 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 2: object foreach
const obj = {
name: 'Jean-Luc Picard',
rank: 'Captain'
};
Object.keys(obj).forEach(key => {
console.log(key, obj[key]);
});
Example 3: javascript foreach in object
for (var key in validation_messages) {
if (!validation_messages.hasOwnProperty(key)) continue;
var obj = validation_messages[key];
for (var prop in obj) {
if (!obj.hasOwnProperty(prop)) continue;
alert(prop + " = " + obj[prop]);
}
}