我无法从数据集中获取最大值和最小值。我认为这个(// Get the largest value from Json object with Javascript与Math.min.apply returns 0 for null相结合)最接近我的需要,但是我的数据有点复杂(至少从我的初学者的观点来看,' )。
我的数据集涉及从1995年到2016年所有国家的预期寿命,如下所示:
[{"country":"Abkhazia","1995":null,"1996":null,"1997":null,"1998":null,"1999":null,"2000":null,"2001":null,"2002":null,"2003":null,"2004":null,"2005":null,"2006":null,"2007":null,"2008":null,"2009":null,"2010":null},
{"country":"Afghanistan","1995":null,"1996":null,"1997":null,"1998":null,"1999":null,"2000":null,"2001":null,"2002":5.7,"2003":6.8,"2004":6.4,"2005":6.6,"2006":6.8,"2007":7.3,"2008":7.0,"2009":7.6,"2010":7.6},
{"country":"Akrotiri and Dhekelia","1995":null,"1996":null,"1997":null,"1998":null,"1999":null,"2000":null,"2001":null,"2002":null,"2003":null,"2004":null,"2005":null,"2006":null,"2007":null,"2008":null,"2009":null,"2010":null},
{"country":"Albania","1995":2.6,"1996":4.0,"1997":4.8,"1998":5.3,"1999":5.8,"2000":6.4,"2001":6.0,"2002":6.3,"2003":6.2,"2004":6.9,"2005":6.8,"2006":6.7,"2007":6.9,"2008":6.7,"2009":6.9,"2010":6.5},
etc.
我需要什么:
完整数据集的最大值和最小值 例如,这将是:
min: 2.6
max: 7.6
我正在使用D3创建数据图,我希望使用这些值来创建一系列颜色。
代码我试过
正如在介绍中所说,我认为我需要上面给出的两个链接的组合,但是我不能使它工作。例如,使用第二个链接I van获得给定国家/地区的最小值,但不是所有国家/地区的最小值。
希望有人可以帮助我!
答案 0 :(得分:3)
您可以遍历所有国家/地区和所有密钥,并检查该值是否不等于null
。然后获取min
和max
值。
var data = [{ "country": "Abkhazia", "1995": null, "1996": null, "1997": null, "1998": null, "1999": null, "2000": null, "2001": null, "2002": null, "2003": null, "2004": null, "2005": null, "2006": null, "2007": null, "2008": null, "2009": null, "2010": null }, { "country": "Afghanistan", "1995": null, "1996": null, "1997": null, "1998": null, "1999": null, "2000": null, "2001": null, "2002": 5.7, "2003": 6.8, "2004": 6.4, "2005": 6.6, "2006": 6.8, "2007": 7.3, "2008": 7.0, "2009": 7.6, "2010": 7.6 }, { "country": "Akrotiri and Dhekelia", "1995": null, "1996": null, "1997": null, "1998": null, "1999": null, "2000": null, "2001": null, "2002": null, "2003": null, "2004": null, "2005": null, "2006": null, "2007": null, "2008": null, "2009": null, "2010": null }, { "country": "Albania", "1995": 2.6, "1996": 4.0, "1997": 4.8, "1998": 5.3, "1999": 5.8, "2000": 6.4, "2001": 6.0, "2002": 6.3, "2003": 6.2, "2004": 6.9, "2005": 6.8, "2006": 6.7, "2007": 6.9, "2008": 6.7, "2009": 6.9, "2010": 6.5 }],
min = Number.MAX_VALUE,
max = -Number.MAX_VALUE;
data.forEach(function (o) {
Object.keys(o).forEach(function (k) {
if (k !== 'country' && o[k] !== null) {
min = Math.min(min, o[k]);
max = Math.max(max, o[k]);
}
});
});
console.log(min, max);

答案 1 :(得分:1)
如果您被允许使用ES6:
const maxValue = dataset.reduce( (max, obj) => {
let ar = [max];
for(const key in obj) {
if (isNaN(Number.parseFloat(obj[key]))) continue;
ar.push(obj[key]):
}
return Math.max.apply(ar, Math);
}, -1)
答案 2 :(得分:0)
我假设您要检查数组的所有元素是否有效,并获得结果数字集的最小值/最大值。这是一个代码示例。您可以单击“运行”以查看结果。
data = [{"country":"Abkhazia","1995":null,"1996":null,"1997":null,"1998":null,"1999":null,"2000":null,"2001":null,"2002":null,"2003":null,"2004":null,"2005":null,"2006":null,"2007":null,"2008":null,"2009":null,"2010":null},
{"country":"Afghanistan","1995":null,"1996":null,"1997":null,"1998":null,"1999":null,"2000":null,"2001":null,"2002":5.7,"2003":6.8,"2004":6.4,"2005":6.6,"2006":6.8,"2007":7.3,"2008":7.0,"2009":7.6,"2010":7.6},
{"country":"Akrotiri and Dhekelia","1995":null,"1996":null,"1997":null,"1998":null,"1999":null,"2000":null,"2001":null,"2002":null,"2003":null,"2004":null,"2005":null,"2006":null,"2007":null,"2008":null,"2009":null,"2010":null},
{"country":"Albania","1995":2.6,"1996":4.0,"1997":4.8,"1998":5.3,"1999":5.8,"2000":6.4,"2001":6.0,"2002":6.3,"2003":6.2,"2004":6.9,"2005":6.8,"2006":6.7,"2007":6.9,"2008":6.7,"2009":6.9,"2010":6.5}];
values = [];
for (i in data) {
for (j in data[i]) {
if (parseFloat(data[i][j]) > 0) {
values.push(parseFloat(data[i][j]));
}
}
}
min = Math.min.apply(null, values);
max = Math.max.apply(null, values);
alert(values)
alert(min);
alert(max);