[javascript] How to convert an Object {} to an Array [] of key-value pairs in JavaScript

I want to convert an object like this:

{"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}

into an array of key-value pairs like this:

[[1,5],[2,7],[3,0],[4,0]...].

How can I convert an Object to an Array of key-value pairs in JavaScript?

This question is related to javascript arrays type-conversion

The answer is


Use Object.entries to get each element of Object in key & value format, then map through them like this:

_x000D_
_x000D_
var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}_x000D_
_x000D_
var res = Object.entries(obj).map(([k, v]) => ([Number(k), v]));_x000D_
_x000D_
console.log(res);
_x000D_
_x000D_
_x000D_

But, if you are certain that the keys will be in progressive order you can use Object.values and Array#map to do something like this:

_x000D_
_x000D_
var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}; _x000D_
_x000D_
                        // idx is the index, you can use any logic to increment it (starts from 0)_x000D_
let result = Object.values(obj).map((e, idx) => ([++idx, e]));_x000D_
_x000D_
console.log(result);
_x000D_
_x000D_
_x000D_


Object.entries() returns an array whose elements are arrays corresponding to the enumerable property [key, value] pairs found directly upon object. The ordering of the properties is the same as that given by looping over the property values of the object manually.

- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries#Description

The Object.entries function returns almost the exact output you're asking for, except the keys are strings instead of numbers.

_x000D_
_x000D_
const obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0};_x000D_
_x000D_
console.log(Object.entries(obj));
_x000D_
_x000D_
_x000D_

If you need the keys to be numbers, you could map the result to a new array with a callback function that replaces the key in each pair with a number coerced from it.

_x000D_
_x000D_
const obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0};_x000D_
_x000D_
const toNumericPairs = input => {_x000D_
    const entries = Object.entries(input);_x000D_
    return entries.map(entry => Object.assign(entry, { 0: +entry[0] }));_x000D_
}_x000D_
_x000D_
console.log(toNumericPairs(obj));
_x000D_
_x000D_
_x000D_

I use an arrow function and Object.assign for the map callback in the example above so that I can keep it in one instruction by leveraging the fact that Object.assign returns the object being assigned to, and a single instruction arrow function's return value is the result of the instruction.

This is equivalent to:

entry => {
    entry[0] = +entry[0];
    return entry;
}

As mentioned by @TravisClarke in the comments, the map function could be shortened to:

entry => [ +entry[0], entry[1] ]

However, that would create a new array for each key-value pair, instead of modifying the existing array in place, hence doubling the amount of key-value pair arrays created. While the original entries array is still accessible, it and its entries will not be garbage collected.

Now, even though using our in-place method still uses two arrays that hold the key-value pairs (the input and the output arrays), the total number of arrays only changes by one. The input and output arrays aren't actually filled with arrays, but rather references to arrays and those references take up a negligible amount of space in memory.

  • Modifying each key-value pair in-place results in a negligible amount of memory growth, but requires typing a few more characters.
  • Creating a new array for each key-value pair results in doubling the amount of memory required, but requires typing a few less characters.

You could go one step further and eliminate growth altogether by modifying the entries array in-place instead of mapping it to a new array:

_x000D_
_x000D_
const obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0};_x000D_
_x000D_
const toNumericPairs = input => {_x000D_
  const entries = Object.entries(obj);_x000D_
  entries.forEach(entry => entry[0] = +entry[0]);_x000D_
  return entries;_x000D_
}_x000D_
_x000D_
console.log(toNumericPairs(obj));
_x000D_
_x000D_
_x000D_


This is my simple barebone implementation:

let obj = {
  "1": 5,
  "2": 7,
  "3": 0,
  "4": 0,
  "5": 0,
  "6": 0,
  "7": 0,
  "8": 0,
  "9": 0,
  "10": 0,
  "11": 0,
  "12": 0
};    

const objectToArray = obj => {
      let sol = [];
      for (key in obj) {
        sol.push([key, obj[key]]);
      }
      return sol;
    };

objectToArray(obj)

I would suggest this simplest solution to use Object.entries()

_x000D_
_x000D_
var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}_x000D_
var result =Object.entries(obj)_x000D_
_x000D_
console.log(result);
_x000D_
_x000D_
_x000D_


The best way is to do:

var obj ={"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10"??:0,"11":0,"12":0} 
Object.entries(obj);

Calling entries, as shown here, will return [key, value] pairs, as the asker requested.

Alternatively, you could call Object.values(obj), which would return only values.


you can use _.castArray(obj).

example: _.castArray({ 'a': 1 }); // => [{ 'a': 1 }]


If you are using lodash, it could be as simple as this:

var arr = _.values(obj);

With lodash, in addition to the answer provided above, you can also have the key in the output array.

Without the object keys in the output array

for:

const array = _.values(obj);

If obj is the following:

{ “art”: { id: 1,  title: “aaaa” }, “fiction”: { id: 22,  title: “7777”} }

Then array will be:

[ { id: 1, title: “aaaa” }, { id: 22, title: “7777” } ]

With the object keys in the output array

If you write instead ('genre' is a string that you choose):

const array= _.map(obj, (val, id) => {
    return { ...val, genre: key };
  });

You will get:

[ 
  { id: 1, title: “aaaa” , genre: “art”}, 
  { id: 22, title: “7777”, genre: “fiction” }
]

To recap some of these answers now on 2018, where ES6 is the standard.

Starting with the object:

let const={"1":9,"2":8,"3":7,"4":6,"5":5,"6":4,"7":3,"8":2,"9":1,"10":0,"12":5};
  • Just blindly getting the values on an array, do not care of the keys:

_x000D_
_x000D_
const obj={"1":9,"2":8,"3":7,"4":6,"5":5,"6":4,"7":3,"8":2,"9":1,"10":0,"12":5};_x000D_
console.log(Object.values(obj));_x000D_
//[9,8,7,6,5,4,3,2,1,0,5]
_x000D_
_x000D_
_x000D_

  • Simple getting the pairs on an array:

_x000D_
_x000D_
const obj={"1":9,"2":8,"3":7,"4":6,"5":5,"6":4,"7":3,"8":2,"9":1,"10":0,"12":5};_x000D_
console.log(Object.entries(obj));_x000D_
//[["1",9],["2",8],["3",7],["4",6],["5",5],["6",4],["7",3],["8",2],["9",1],["10",0],["12",5]]
_x000D_
_x000D_
_x000D_

  • Same as previous, but with numeric keys on each pair:

_x000D_
_x000D_
const obj={"1":9,"2":8,"3":7,"4":6,"5":5,"6":4,"7":3,"8":2,"9":1,"10":0,"12":5};_x000D_
console.log(Object.entries(obj).map(([k,v])=>[+k,v]));_x000D_
//[[1,9],[2,8],[3,7],[4,6],[5,5],[6,4],[7,3],[8,2],[9,1],[10,0],[12,5]]
_x000D_
_x000D_
_x000D_

  • Using the object property as key for a new array (could create sparse arrays):

_x000D_
_x000D_
const obj={"1":9,"2":8,"3":7,"4":6,"5":5,"6":4,"7":3,"8":2,"9":1,"10":0,"12":5};_x000D_
console.log(Object.entries(obj).reduce((ini,[k,v])=>(ini[k]=v,ini),[]));_x000D_
//[undefined,9,8,7,6,5,4,3,2,1,0,undefined,5]
_x000D_
_x000D_
_x000D_

This last method, it could also reorganize the array order depending the value of keys. Sometimes this could be the desired behaviour (sometimes don't). But the advantage now is that the values are indexed on the correct array slot, essential and trivial to do searches on it.

  • Map instead of Array

Finally (not part of the original question, but for completeness), if you need to easy search using the key or the value, but you don't want sparse arrays, no duplicates and no reordering without the need to convert to numeric keys (even can access very complex keys), then array (or object) is not what you need. I will recommend Map instead:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

let r=new Map(Object.entries(obj));
r.get("4"); //6
r.has(8); //true

You can use Object.values([]), you might need this polyfill if you don't already:

const objectToValuesPolyfill = (object) => {
  return Object.keys(object).map(key => object[key]);
};
Object.values = Object.values || objectToValuesPolyfill;

https://stackoverflow.com/a/54822153/846348

Then you can just do:

var object = {1: 'hello', 2: 'world'};
var array = Object.values(object);

Just remember that arrays in js can only use numerical keys so if you used something else in the object then those will become `0,1,2...x``

It can be useful to remove duplicates for example if you have a unique key.

var obj = {};
object[uniqueKey] = '...';

In Ecmascript 6,

var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0};

var res = Object.entries(obj);

console.log(res);

fiddle


Recursive convert object to array

function is_object(mixed_var) {
    if (mixed_var instanceof Array) {
        return false;
    } else {
        return (mixed_var !== null) && (typeof( mixed_var ) == 'object');
    }
}


function objectToArray(obj) {
    var array = [], tempObject;
    for (var key in obj) {

        tempObject = obj[key];

        if (is_object(obj[key])) {
            tempObject = objectToArray(obj[key]);
        }
        array[key] = tempObject;
    }
    return array;
}

Use for in

var obj = { "10":5, "2":7, "3":0, "4":0, "5":0, "6":0, "7":0,
            "8":0, "9":0, "10":0, "11":0, "12":0 };

var objectToArray = function(obj) {
    var _arr = [];

    for (var key in obj) {
        _arr.push([key, obj[key]]);
    }
    return _arr;
}

console.log(objectToArray(obj));

We can change Number to String type for Key like below:

_x000D_
_x000D_
var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}_x000D_
var result = Object.keys(obj).map(function(key) {_x000D_
  return [String(key), obj[key]];_x000D_
});_x000D_
    _x000D_
console.log(result);
_x000D_
_x000D_
_x000D_


Use Object.keys and Array#map methods.

_x000D_
_x000D_
var obj = {_x000D_
  "1": 5,_x000D_
  "2": 7,_x000D_
  "3": 0,_x000D_
  "4": 0,_x000D_
  "5": 0,_x000D_
  "6": 0,_x000D_
  "7": 0,_x000D_
  "8": 0,_x000D_
  "9": 0,_x000D_
  "10": 0,_x000D_
  "11": 0,_x000D_
  "12": 0_x000D_
};_x000D_
// get all object property names_x000D_
var res = Object.keys(obj)_x000D_
  // iterate over them and generate the array_x000D_
  .map(function(k) {_x000D_
    // generate the array element _x000D_
    return [+k, obj[k]];_x000D_
  });_x000D_
_x000D_
console.log(res);
_x000D_
_x000D_
_x000D_


This is my solution, i have the same issue and its seems like this solution work for me.

yourObj = [].concat(yourObj);

Yet another solution if Object.entries won't work for you.

_x000D_
_x000D_
const obj = {_x000D_
      '1': 29,_x000D_
      '2': 42_x000D_
    };_x000D_
const arr = Array.from(Object.keys(obj), k=>[`${k}`, obj[k]]);_x000D_
console.log(arr);
_x000D_
_x000D_
_x000D_


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to type-conversion

How can I convert a char to int in Java? pandas dataframe convert column type to string or categorical How to convert an Object {} to an Array [] of key-value pairs in JavaScript convert string to number node.js Ruby: How to convert a string to boolean Convert bytes to int? Convert dataframe column to 1 or 0 for "true"/"false" values and assign to dataframe SQL Server: Error converting data type nvarchar to numeric How do I convert a Python 3 byte-string variable into a regular string? Leading zeros for Int in Swift