There are 4 ways to iterate over an object keys and values in JavaScript:
- The
for...in
loop is used for iterating over keys of objects, arrays, and strings. - The
Object.keys()
method returns an array of object keys. - The
Object.values()
method returns the values of all properties in the object as an array. - 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.