published: 14 May 2022
2 min read
How to loop through object values using Object.values() in JavaScript
The Object.values()
method was introduced in ES8 and it does the opposite of the Object.key()
method.
The Object.values()
method returns the values of all properties in the object as an array. You can then loop through the values array by using any of the array looping methods.
Here is an example:
const animals = {
tiger: '🐅',
cat: '🐱',
monkey: '🐒',
elephant: '🐘'
}
// iterate over object values
Object.values(animals).forEach(val => console.log(val))
// 🐅
// 🐱
// 🐒
// 🐘
Read this article to learn more about different ways of iterating over an object in JavaScript.
Are we missing something? Help us improve this article. Reach out to us.
How to loop through object values using Object.values() in JavaScript
The Object.values()
method was introduced in ES8 and it does the opposite of the Object.key()
method.
The Object.values()
method returns the values of all properties in the object as an array. You can then loop through the values array by using any of the array looping methods.
Here is an example:
const animals = {
tiger: '🐅',
cat: '🐱',
monkey: '🐒',
elephant: '🐘'
}
// iterate over object values
Object.values(animals).forEach(val => console.log(val))
// 🐅
// 🐱
// 🐒
// 🐘
Read this article to learn more about different ways of iterating over an object in JavaScript.
Are you looking for other code tips?
JS Nooby
Javascript connoisseur