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) // [28, 17, 54]

Example 4: javascript foreach in object

for (var key in validation_messages) {
    // skip loop if the property is from prototype
    if (!validation_messages.hasOwnProperty(key)) continue;

    var obj = validation_messages[key];
    for (var prop in obj) {
        // skip loop if the property is from prototype
        if (!obj.hasOwnProperty(prop)) continue;

        // your code
        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]}`);
}