My question seems so simple to me that I cannot figure why I could not find an answer on the web. Really sorry in advance if this has been answered a thousand times, maybe I just don't have the wording right.
I am working with json formatted data in d3.js.
dataset = [
{"category":"Fiction", "popularity":20},
{"category":"Thriller", "popularity":45},
{"category":"Romance", "popularity":12}
];
From all I know, some of the d3.js functions require a simple array.
Example 1: for a bar chart, my d3.scale.ordinal().domain(data) needs an array with ["Fiction", "Thriller", "Romance"]
Example 2: for a pie chart, the d3.layout.pie(data) function needs an array with [20,45,12]
These functions force me to extract all the values from "category" or "popularity" into one-dimensional array via simple for loop, which seems quite dumb.
Is there a more elegant and quick solution to extract an array with the values of one variable from a json?
Thanks a lot for your help! Xavier
.map()to extract the values, but you'd need two.map()statements in a row to create both the["Fiction","Thriller","Romance"]and[20,45,12]arrays, so I would guess a simpleforloop would be more efficient.