how to loop through object in vanilla js code example

Example 1: javascript loop through object

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

Example 2: how to loop trough an object java script

// Looping through arrays created from Object.keys
const keys = Object.keys(fruits)
for (const key of keys) {
  console.log(key)
}

// Results:
// apple
// orange
// pear

Example 3: loop through object javascript

for (var property in object) {
  if (object.hasOwnProperty(property)) {
    // Do things here
  }
}

Example 4: loop an object in javascript

var obj = { first: "John", last: "Doe" };

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

Example 5: 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 6: js loop through object

for (var property in object) {
  if (object.hasOwnProperty(property)) {
    // Do things here
  }
}