How to iterate over object keys and values in JavaScript

There are 4 ways to iterate over an object keys and values in JavaScript:

  1. The for...in loop is used for iterating over keys of objects, arrays, and strings.
  2. The Object.keys() method returns an array of object keys.
  3. The Object.values() method returns the values of all properties in the object as an array.
  4. The Object.entries() method returns an array of the object's key-value pairs.

The Object.keys() method was added in ES6, whereas, Object.entries() and Object.values() methods were added in ES8. These methods convert the object into an array and then use array looping methods to iterate over that array.

Iterate over object using for...in loop

The simplest and most popular way to iterate over an object's keys and values is using the for...in loop:

const birds = {
  owl: '🦉',
  eagle: '🦅',
  duck: '🦆',
  chicken: '🐔'
}

for (const key in birds) {
  console.log(`${key} -> ${birds[key]}`)
}

// owl -> 🦉
// eagle -> 🦅
// duck -> 🦆
// chicken -> 🐔

The for...in loop works in all modern and old browsers, including Internet Explorer 6+.

The only issue with the for...in loop is it iterates through the properties in the prototype chain.

Since the JavaScript object inherits properties from its prototype, the for...in loop will iterate over those properties as well.

However, you can use the hasOwnProperty() method to exclude inherited properties:

for (const key in birds) {
  if (birds.hasOwnProperty(key)) {
    console.log(`${key} -> ${birds[key]}`)
  }
}

Iterate over object using Object.keys() method

The Object.keys() method takes an object as input and returns an array of the object's own enumerable properties names:

const birds = {
  owl: '🦉',
  eagle: '🦅',
  duck: '🦆',
  chicken: '🐔'
}

const keys = Object.keys(birds)

console.log(keys)
// [ 'owl', 'eagle', 'duck', 'chicken' ]

Now we can use the forEach() loop to iterate over the array and retrieve the value of each property:

keys.forEach(key => {
  console.log(`${key} -> ${birds[key]}`)
})

// owl -> 🦉
// eagle -> 🦅
// duck -> 🦆
// chicken -> 🐔

Iterate over object using Object.values() method

The Object.values() method, unlike Object.keys(), returns an array of the given object's own enumerable properties values:

const birds = {
  owl: '🦉',
  eagle: '🦅',
  duck: '🦆',
  chicken: '🐔'
}

Object.values(birds).forEach(item => console.log(item))

// 🦉
// 🦅
// 🦆
// 🐔

Iterate over object using Object.entries() method

The Object.entries() method returns an array of arrays, whereby each nested array has two elements. The first element is the property and the second element is the value.

const birds = {
  owl: '🦉',
  eagle: '🦅',
  duck: '🦆',
  chicken: '🐔'
}

const entries = Object.entries(birds)
console.log(entries)

// [
//     [ 'owl', '🦉' ],
//     [ 'eagle', '🦅' ],
//     [ 'duck', '🦆' ],
//     [ 'chicken', '🐔' ]
// ]

To iterate over the nested array returned by Object.entries(), use the for...of loop or the forEach() method as shown below:

// => `for...of` Loop
for (const [key, value] of Object.entries(birds)) {
  console.log(`${key} -> ${value}`)
}

// => `forEach()` Loop
Object.entries(birds).forEach(([key, value]) => {
  console.log(`${key} -> ${value}`)
})

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.