92

Given a JavaScript object:

var dataObject = {
   object1: {id: 1, name: "Fred"}, 
   object2: {id: 2, name: "Wilma"}, 
   object3: {id: 3, name: "Pebbles"}
};

How do I efficiently extract the inner objects into an array? I do not need to maintain a handle on the object[n] IDs.

var dataArray = [
    {id: 1, name: "Fred"}, 
    {id: 2, name: "Wilma"}, 
    {id: 3, name: "Pebbles"}]

14 Answers 14

87
var dataArray = Object.keys(dataObject).map(function(k){return dataObject[k]});
5
  • 2
    @Qix: You mean so much faster?
    – julien
    Nov 17, 2014 at 12:30
  • 1
    @julien No. Not at all. Not even close. Accepted answer is faster in every case always. Nov 17, 2014 at 22:24
  • 1
    @Qix: And for (var o = 0, l = dataObject.length; o < l; o++) {} would be even faster than for ... in. Although, map could be quicker when the function inside the map is very complicated, and is processed using multiple threads and webworkers... but yes, in almost all cases a for loop is much faster.
    – okdewit
    Jun 3, 2015 at 12:34
  • 3
    This is so much safer and idiomatic. Object.keys does a hasOwnProperty internally. Feb 20, 2017 at 8:20
  • 1
    Adding a check for hasOwnProperty to the accepted answer is efficient than this method where an array is built first and it is traversed O(N) space + O(N) time.
    – Murali VP
    Aug 14, 2019 at 20:49
80
var dataArray = [];
for(var o in dataObject) {
    dataArray.push(dataObject[o]);
}
6
  • 13
    And what is "ordering" in your opinion? Nov 11, 2009 at 23:28
  • 12
    If you don't want to include properties from the object's prototype (there shouldn't be any if it's a plain object), you can filter them by checking dataObject.hasOwnProperty(o). Nov 12, 2009 at 1:58
  • 7
    A side note: if for some reason extra properties are added to Object.prototype this will break without using hasOwnProperty().
    – Ja͢ck
    May 24, 2013 at 9:12
  • 3
    Any reason to use new Array here instead of []? Mar 23, 2017 at 18:49
  • 1
    @SLaks since an object is defined as an unordered collection of properties there is no order to preserve.
    – Rune FS
    Jun 25, 2017 at 10:14
39

ES6 version:

var dataArray = Object.keys(dataObject).map(val => dataObject[val]);
0
33

Using underscore:

var dataArray = _.values(dataObject);
1
16

With jQuery, you can do it like this -

var dataArray = $.map(dataObject,function(v){
     return v;
});

Demo

15

ES2017 using Object.values:

const dataObject = {
    object1: {
        id: 1,
        name: "Fred"
    },
    object2: {
        id: 2,
        name: "Wilma"
    },
    object3: {
        id: 3,
        name: "Pebbles"
    }
};

const valuesOnly = Object.values(dataObject);

console.log(valuesOnly)

8

Assuming your dataObject is defined the way you specified, you do this:

var dataArray = [];
for (var key in dataObject)
    dataArray.push(dataObject[key]);

And end up having dataArray populated with inner objects.

3
  • 1
    This also doesn't do what he's asking for.
    – SLaks
    Nov 11, 2009 at 23:28
  • 2
    He wants the objects themselves, not their names.
    – SLaks
    Nov 11, 2009 at 23:31
  • In that case, just omit the ".name" in the third line. Nov 11, 2009 at 23:32
7

Using the accepted answer and knowing that Object.values() is proposed in ECMAScript 2017 Draft you can extend Object with method:

if(Object.values == null) {
    Object.values = function(obj) {
        var arr, o;
        arr = new Array();
        for(o in obj) { arr.push(obj[o]); }
        return arr;
    }
}
6

[Editing and updating my answer. The other answers seem to overlap with mine pretty much, but, I thought I have another ago and provide an alternative].

I present 3 solutions to this problem, based on:

  • Object.keys
  • Object.values
  • Object.entries

Objects.keys() solution:

let keys = Object.keys(dataObject); // ["object1", "object2", "object3" ];
let keysToResult = keys.map( e => dataObject[e] ); // [{"id":1,"name":"Fred"},{"id":2,"name":"Wilma"},{"id":3,"name":"Pebbles"}]

Object.values solution:

let values = Object.values(dataObject); // [{"id":1,"name":"Fred"},{"id":2,"name":"Wilma"},{"id":3,"name":"Pebbles"}]

Object.entries solution:

let entries = Object.entries(dataObject); // [["object1",{"id":1,"name":"Fred"}],["object2",{"id":2,"name":Wilma"}],["object3",{"id":3,"name":"Pebbles"}]]
let entriesToResult = entries.map( ([k,v]) => v ); [{"id":1,"name":"Fred"},{"id":2,"name":"Wilma"},{"id":3,"name":"Pebbles"}]

All three solutions have their own features.

Object.keys() returns an array with insufficient result. So, we use Array.prototype.map to top up each value in the array to get close to what we want. In general, we can think of Object.keys() combined with map as a mechanism to customize our result list with what we want.

Object.values() is interesting since it discards the key and just returns the results only. In fact, for this problem, this is perfect since the answer requires no further processing.

Object.entries() returns more than what we want since it returns both keys and values. We need to use map to customize our result. In fact, we need to cut out the excess information.

Object.keys(), Object.values() and Object.entries() are all very useful functions which is why I wanted to show all 3 as a solution to this problem. Depending on your specific use case, you may find one to a better fit to solving your problem.

5

Maybe a bit verbose, but robust and fast

var result = [];
var keys = Object.keys(myObject);
for (var i = 0, len = keys.length; i < len; i++) {
    result.push(myObject[keys[i]]);
}
4

Object.values() method is now supported. This will give you an array of values of an object.

Object.values(dataObject)

Refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values

1
  • 1
    Looks like a duplicate answer. Aug 23, 2018 at 15:42
2

In case you use d3. you can do d3.values(dataObject) which will give

enter image description here

2

I prefer to destruct object values into array:

[...Object.values(dataObject)]

var dataObject = {
   object1: {id: 1, name: "Fred"}, 
   object2: {id: 2, name: "Wilma"}, 
   object3: {id: 3, name: "Pebbles"}
};

var dataArray = [...Object.values(dataObject)];
0
0

This one worked for me

var dataArray = Object.keys(dataObject).map(function(k){return dataObject[k]});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.