Languages
[Edit]
EN

JavaScript - convert object to array of key-value pairs

0 points
Created by:
Aston-Freeman
787

In this article, we would like to show you how to convert object to array of key-value pairs using JavaScript.

Practical example

In this example, we use keys() method to get keys of the object and then using map() method on those keys we convert object into an array of key-value pairs.

// ONLINE-RUNNER:browser;

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

const array = Object.keys(object).map((key) => [Number(key), object[key]]);

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

 

See also

  1. JavaScript - transform object into array with lodash

References

  1. Object.keys() - JavaScript | MDN

Alternative titles

  1. JavaScript - convert Object {} to Array [] of key-value pairs
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