iterate over object javascript
snippet in javascript

javascript loop through object

user896

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

javascript loop object

user150

let obj = {
  key1: "value1",
  key2: "value2",
  key3: "value3"
}

Object.keys(obj).forEach(key => {
    console.log(key, obj[key]);
});
// key1 value1
// key2 value2
// key3 value3

// using for in - same output as above
for (let key in obj) {
  let value = obj[key];
  console.log(key, value);
}

javascript loop through object

user6012

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

javascript iterate over object

user6928

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

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

iterate over object javascript

user915

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

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

iterate over object javascript

user377

// For a functional one-liner
Object.keys(pokemons).forEach(console.log);
// Bulbasaur
// Charmander
// Squirtle
// Pikachu