loop through object in object javascript code example
Example 1: javascript loop through object
Object.entries(obj).forEach(
([key, value]) => console.log(key, value)
);
Example 2: loop over keys in object javascript
Object.keys(obj).forEach(function(key) {
console.log(key, obj[key]);
});
Example 3: javascript looping through object
const fruits = {
apple: 28,
orange: 17,
pear: 54,
}
const values = Object.values(fruits)
console.log(values)
Example 4: 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]);
}
}
Example 5: js loop through object
const object = { a: 1, b: 2, c: 3 };
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}