Convert An Object To An Array In JavaScript

To convert an object to an array in JavaScript, you can use –

  1. Object.keys() – To get an array containing all the keys of the object.
  2. Object.values() – To get an array containing all the values of the object.
  3. Object.entries() – To get an array containing all the key-value pairs of the object.

Let’s discuss this in detail below.

convert an object to an array in javascript

Convert An Object To An Array In JavaScript Using Object.keys()

The Object.keys() method is used to get an array containing all the keys of the object. The syntax for using this method is –

Object.keys(obj);

Here, obj is the object for which we want to get the keys array. Let’s see some examples of using this method.

Example 1 – Convert Object To Array Using Object.keys()

Consider the following object –

var myObj = { "name":"John", "age":31, "city":"New York" };

To convert this object to an array, we can use the Object.keys() method as follows –

var arr = Object.keys(myObj);

console.log(arr); //will print ['name', 'age', 'city']

As you can see, the Object.keys() method returns an array containing all the keys of the object.

Convert An Object To An Array In JavaScript Using Object.values()

The Object.values() method is used to get an array containing all the values of the object. The syntax for using this method is –

Object.values(obj);

Here, obj is the object for which we want to get the values array. Let’s see some examples of using this method.

Example 2 – Convert Object To Array Using Object.values()

Consider the following object –

var myObj = { "name":"John", "age":31, "city":"New York" };

To convert this object to an array, we can use the Object.values() method as follows –

var arr = Object.values(myObj);

console.log(arr); //will print ['John', 31, 'New York']

As you can see, the Object.values() method returns an array containing all the values of the object.

Convert An Object To An Array In JavaScript Using Object.entries()

The Object.entries() method is used to get an array containing all the key-value pairs of the object. The syntax for using this method is –

Object.entries(obj);

Here, obj is the object for which we want to get the key-value pairs array. Let’s see some examples of using this method.

Example 3 – Convert Object To Array Using Object.entries()

Consider the following object –

var myObj = { "name":"John", "age":31, "city":"New York" };

To convert this object to an array, we can use the Object.entries() method as follows –

var arr = Object.entries(myObj);

console.log(arr); //will print [['name', 'John'], ['age', 31], ['city', 'New York']]

As you can see, the Object.entries() method returns an array containing all the key-value pairs of the object.

Thus, in this article, we saw how to convert an object to an array in JavaScript using the Object.keys() method, the Object.values() method, and the Object.entries() method.

What method you use depends on what output you want. If you want an array of keys, use Object.keys(). If you want an array of values, use Object.values(). And if you want an array of key-value pairs, use Object.entries().

I hope this article was helpful to you. Thank you for reading! 🙂

Leave a Reply