how to loop over an object in js code example

Example 1: javascript loop through object

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

Example 2: object loop in javascript

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

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

Example 3: looping through object javascript

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

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

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

Example 4: how to loop trough an object java script

const fruits = {
  apple: 28,
  orange: 17,
  pear: 54,
}

const values = Object.values(fruits)
console.log(values) // [28, 17, 54]

Example 5: iterate over array of object javascript and access the properties

for (let item of items) {
    console.log(item); // Will display contents of the object inside the array
}