Languages
[Edit]
EN

JavaScript - convert object to array

0 points
Created by:
Jun-L
963

In this article, we would like to show you how to convert object to array in JavaScript.

To convert an object to an array we can use one of the following methods:

  1. Object.keys() - converts object keys into an array,
  2. Object.values() - converts object values into an array,
  3. Object.entries() - converts enumerable string-keyed properties [key, value] pairs of an object into an array.

Note:

The Object.keys() method has been introduced in ES6, and the Object.values() and Object.entries() in ES8.

1. Convert object keys to array

In this example, we use Object.keys() method to convert keys of the object into array.

// ONLINE-RUNNER:browser;

const object = {
    1: 'a',
    2: 'b',
    3: 'c',
};

const array = Object.keys(object);

console.log(JSON.stringify(array)); // [ '1', '2', '3' ]

2. Convert object values to array

In this example, we use Object.values() method to convert values of the object into array.

// ONLINE-RUNNER:browser;

const object = {
    1: 'a',
    2: 'b',
    3: 'c',
};

const array = Object.values(object);

console.log(JSON.stringify(array)); // [ 'a', 'b', 'c' ]

 

3. Convert object entries (key-value pairs) to array

In this example, we use Object.entries() method to convert enumerable string-keyed properties [key, value] pairs of the object into array.

// ONLINE-RUNNER:browser;

const object = {
    1: 'a',
    2: 'b',
    3: 'c',
};

const array = Object.entries(object);

console.log(JSON.stringify(array)); // [ [ '1', 'a' ], [ '2', 'b' ], [ '3', 'c' ] ]

 

See also

  1. JavaScript - convert object to array of key-value pairs

References

  1. Object.keys() - JavaScript | MDN
  2. Object.values() - JavaScript | MDN
  3. Object.entries() - JavaScript | MDN

Alternative titles

  1. JavaScript - converting object with numeric keys into array
  2. JavaScript - convert Object to Array using ES6 features
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join