EN
JavaScript - convert object to array of key-value pairs
0
points
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' ] ]