Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing properties of an array of objects [duplicate]

pretty basic question I think, but I couldn't find info on that.

Through d3 I parse a csv and each object look like this

name: "whatever"
number: "52"

How can I access the array of all the properties "number" as an array without creating a new array and pushing each element?

like image 379
Dave Avatar asked Nov 26 '11 21:11

Dave


2 Answers

ES6 version:

const numbers = objects.map( o => o.number );

Enjoy.

like image 63
Francois Nadeau Avatar answered Oct 29 '22 06:10

Francois Nadeau


Use array.map:

var numbers = objects.map(function(o) { return o.number; });
like image 20
mbostock Avatar answered Oct 29 '22 07:10

mbostock