loop over object keys code example

Example 1: iterate over object javascript

// 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 2: loop over keys in object javascript

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

Example 3: object loop in javascript

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

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

Example 4: loop key in object

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

for(key in fruits){
	console.log(key)
}

Example 5: js loop through object

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