gpt4 book ai didi

javascript - 如何在嵌套对象数组中查找范围?

转载 作者:行者123 更新时间:2023-11-30 13:10:03 25 4
gpt4 key购买 nike

我有一个嵌套对象数组,其中包含每个州每年的 GDP、收入、人口等值:

// 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 : [
[...]
]
}, {
...
}
]

现在,我想找出所有州和年份的每个维度的最小值和最大值(范围)。

populationExtents = [659651, 82536680];
gdpExtents = [14250, 2498800];
incomeExtents = [..., ...];

我怎样才能做到这一点而不必多次遍历整个数组?目前,我正在为每个维度执行此操作:

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];
});
});

但我认为这太复杂了,因为我应该能够计算每个维度和状态的所有“局部”最小值,然后计算所有状态的全局最小值(而不是每个维度一次通过) ).

我尝试了多个级别的 d3.map 和嵌套的 d3.min,但我无法理解这个结构。

最佳答案

function getMaxMin( prop ) {
var concat = [].concat,
arr = concat.apply([], records.map(function(value) {
return concat.apply([], value[prop]);
}));

return [ Math.min.apply(Math.min, arr), Math.max.apply(Math.max, arr) ];
}

或者更漂亮一点:

function getMaxMin( prop ) {
var arr = [];

records.map(function(value) {
arr = arr.concat.apply(arr, value[prop][1]);
});

return [ Math.min.apply(Math.min, arr), Math.max.apply(Math.max, arr) ];
}

编辑:要排除年份 [year, value] 并将几乎所有内容置于同一个循环中:

function getMaxMin() {
var arrs = [];

records.map(function(value) {
arrs[0] = arrs[0].concat(value.income);
arrs[1] = arrs[1].concat(value.GDP);
arrs[2] = arrs[2].concat(value.population);
});

arrs[0] = arrs[0].filter(c);
arrs[1] = arrs[1].filter(c);
arrs[2] = arrs[2].filter(c);

function c(value, key) {
return key % 2;
}

return [
[ Math.min.apply(Math.min, arrs[0]), Math.max.apply(Math.max, arrs[0]) ],
[ Math.min.apply(Math.min, arrs[1]), Math.max.apply(Math.max, arrs[1]) ],
[ Math.min.apply(Math.min, arrs[2]), Math.max.apply(Math.max, arrs[2]) ]
];
}

var maxMin = getMaxMin();

maxMin === [
[income-min, income-max],
[GDP-min, GDP-max],
[population-min, population-max]
]

演示:http://jsbin.com/ecineg/1/embed?javascript,console

关于javascript - 如何在嵌套对象数组中查找范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14216764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com