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"}]
Maybe a bit verbose, but robust and fast
Assuming your dataObject is defined the way you specified, you do this:
And end up having dataArray populated with inner objects.
ES6 version:
Object.values() method is now supported. This will give you an array of values of an object.
Refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values
ES2017 using
Object.values
:In case you use d3. you can do
d3.values(dataObject)
which will give