Converting an Object to an Array

Converting an Object to an Array

Suppose We have an Object. The object is given below:

Normal Object Printing

The object name is 'avengers'. Now we are given a task to create an array from it. We can create three different arrays from it.

We can create different arrays from one object. Here are examples:

Converting Keys of an object to an array

By using the 'Object.keys()' - method, we can create an array by only using keys of the object. We just simply need to pass the object name in the bracket. This will create an array with the keys of the object.

Converting Values of an object to an array

By using the 'Object.values()' - method, we can create an array by only using values of the keys of the object. We just simply need to pass the object name in the bracket. This will create an array with the values of the keys of the object.

Now Suppose we want to create an array from this object consisting of enumerable property [key, value] pair. This can also be done by using a simple method.

Getting an key, value pair from an object

'Object.entries()' - method are used to create an array with [key, value] pair.


This is a simple blog about how we can create 3 different types of array from an single object.

To view or add a comment, sign in

More articles by Md. Tahmid Chowdhury

  • Higher Order Components (HOC)

    What is Higher Order Component(HOC)? It is an advanced method of reusing the component functionality logic. It takes…

    1 Comment
  • An Array and Object Method

    In this article let us talk about a simple method about an array containing objects. In the above picture, we can see…

  • Node.js Stream

    Streams is a pure NodeJS concept. Nodejs is a runtime environment and it uses JavaScript.

    2 Comments

Explore content categories