js object iterate keys code example

Example 1: iterate key value object javascript

'use strict';
// ECMAScript 2017
const object = {'a': 1, 'b': 2, 'c' : 3};
for (const [key, value] of Object.entries(object)) {
  console.log(key, value);
}

Example 2: javascript iterate object key values

Object.entries(obj).forEach(([key, value]) => {
	console.log(key, value);
});

Example 3: js loop over object

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

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

Example 4: javascript iterate over object keys and values

const object1 = {
  a: 'somestring',
  b: 42
};

for (let [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

// expected output:
// "a: somestring"
// "b: 42"
// order is not guaranteed

Example 5: javascript loop through object values

var myObj = {foo: "bar", baz: "baz"};
Object.values(myObj).map((val) => {
console.log(val);
})
// "bar" "baz"