问题
I have an object array, called dataArray, where one object is like this:
{
id,
x,
y
}
now I want to have the lower quantile of this array by using d3. I thought about something like this:
quartile = d3.quantile(dataArray, 0.25, function(d) {return d.y})
but unfortunately there is no accessor option for this method. How can I get the quantile of a property from an object array ? Thanks in advance
回答1:
I found the solution to my problem. I need to get an array of only a certain property of an array of objects, so I don't need an accessor within the quantile method.
// get an array of just the y property values
var keyArray = dataArray.map(function(item) { return item["y"]; });
// calculate a lower quantile of this array
var quantile_y = d3.quantile(keyArray, 0.25);
UPDATE:
In order to work correctly, the array given to d3.quantile() has to be sorted. e.g.
keyArray.sort(d3.ascending);
来源:https://stackoverflow.com/questions/31612640/how-to-get-quantile-of-object-array