Convert Javascript Object to an Array

Convert Javascript Object to an Array

ยท

1 min read

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]]
ย