Converting JavaScript Object To An Array

Ridwan Yusuf
1 min readMay 24, 2021

--

Ways To Convert JavaScript Objects To Array

Converting Objects in JavaScript gives the chance to have access to some cool methods(like forEach(), filter(), etc) available while using an array.

In this tutorials, we will be looking into three techniques which objects can be changed in to array using any of these three methods: Object.keys(), Object.values(), and Object.entries()

Say we have a cart object defined as follow:

cart = {
orange : 15,
mango : 15,
vegetable : 15,
}

Using Object.keys():

We simply pass the name of the object into Object.keys(). Keys in the objects are converted into array

cart = {
orange : 15,
mango : 15,
vegetable : 15,
}
newCartArray = Object.keys(cart)
console.log(newCartArray)
// [ 'orange', 'mango', 'vegetable' ]

Using Object.values():

The values JavaScript objects are converted into an array using Object.values().

cart = {
orange : 15,
mango : 15,
vegetable : 15,
}
newCartArray = Object.values(cart)
console.log(newCartArray)
// [ 15, 15, 15 ]

Using Object.entries():

Object.entries() method makes it easy to get both key and value from the Object.

cart = {
orange : 15,
mango : 15,
vegetable : 15,
}
newCartArray = Object.entries(cart)
console.log(newCartArray)
// [ [ 'orange', 15 ], [ 'mango', 15 ], [ 'vegetable', 15 ] ]

Enjoyed this article?

Feel free to explore my video collection on YouTube for more educational content. Don’t forget to subscribe to the channel to stay updated with future releases.

--

--

Ridwan Yusuf
Ridwan Yusuf

Written by Ridwan Yusuf

RidwanRay.com -I provide engineers with actionable tips and guides for immediate use

No responses yet