I have an array of nested objects containing values like GDP, income, population etc. per year per state:
// The "records" array looks like this:
[
{
name : "...",
income : [
[1995, 1234], // [year, value]
[1996. 1235],
[...]
],
GDP : [
[1995, 1234],
[1996. 1235],
[...]
],
population : [
[1995, 1234],
[1996. 1235],
[...]
]
}, {
name : "...",
income : [
[...]
],
GDP : [
[...]
],
population : [
[...]
]
}, {
...
}
]
Now, I want to find the minimum and maximum (extent) for each dimension over all states and years.
populationExtents = [659651, 82536680];
gdpExtents = [14250, 2498800];
incomeExtents = [..., ...];
How can I do this without having to traverse the whole array multiple times? Currently, I’m doing this for each dimension:
var income = records.map(function(d, i) {
return d.income;
});
var min = d3.min(income, function(d, i) {
return d3.min(d, function(e) {
return e[1]; // returns values of each year
});
});
var max = d3.max(income, function(d, i) {
return d3.max(d, function(e) {
return e[1];
});
});
But I think this is way too complex, since I should be able to calculate all “local” minimums per dimension and state, and then calculate the global minimum over all states in just on pass (instead of one pass per each dimension).
I tried several levels of d3.map and nested d3.min, but am not able to wrap my head around this structure.
Or a little prettier:
EDIT: To exclude year
[year, value]and putting almost everything under the same loop:Demo: http://jsbin.com/ecineg/1/embed?javascript,console