After ES2017 released. We have 3 variations to convert an Object to an Array ๐
It was a bit lengthy to convert Object
to an Array
in Javascript before ES6
so we had to do something like this ๐
var animals = {
cat: 1,
dog: 2,
};
var keys = [];
for (var animal in animals) {
if (animals.hasOwnProperty(animal)) {
keys.push(animal);
}
}
keys; // ["cat", "dog"]
ES6 ๐
Later ES6
Introduced and we got easiest way
Now, there is a built-in method that quickly turns all the keys of object into an array:
1. Object.keys ๐คฉ
var animals = {
cat: 1,
dog: 2,
};
const keys = Object.keys(animals);
keys; // ["cat", "dog"]
2. Object.values ๐ค
Using Object.values
we can extract the values into an array with one method.
var animals = {
cat: 1,
dog: 2,
};
const values = Object.values(animals);
values;
// [1,2]
3. Object.entries ๐
using Object.entries
now we will get both (keys and values) now ๐ฅณ
var animals = {
cat: 1,
dog: 2,
};
const entries = Object.entries(animals);
entries; // [['cat':1],['dog':2]]
ย