js object key loop code example

Example 1: loop over keys in object javascript

Object.keys(obj).forEach(function(key) {
  console.log(key, obj[key]);
});

Example 2: javascript for key in object

// iterates over all enumerable properties of an object that are 
// keyed by strings (ignoring ones keyed by Symbols), 
// including inherited enumerable properties.

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

// expected output:
// "a: 1"
// "b: 2"
// "c: 3"