I'm having trouble getting the maximum and the minimum values out of my dataset. I think this (// Get the largest value from Json object with Javascript combined with Math.min.apply returns 0 for null) comes closest to what I need, however my data is a bit more complicated (at least from my 'beginners point of view').
我无法从数据集中获得最大值和最小值。我认为这(//通过Javascript结合Math.min从Json对象中获得最大的值。对于null, apply返回0)最接近于我所需要的,但是我的数据有点复杂(至少从我的“初学者视角”来看)。
my dataset concerns life expectancy for all countries from 1995 till 2016 and looks like this:
从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.
What I need:
我所需要的东西:
The maximum value and the minimum value for the complete dataset.
For the example this would be:
完整数据集的最大值和最小值。例如:
min: 2.6
max: 7.6
I'm making a datamap using D3 and I want to use the values to create a range of colors.
我正在使用D3创建一个datamap,我想使用这些值创建一个颜色范围。
Code I tried
代码我试着
As said in the introduction I think I need a combination of the two links given above, however I cannot make it work. For example, using the second link I van get the minimum value for a given country, but not the minimum of all countries.
正如引言中所说,我认为我需要结合上面给出的两个链接,但是我不能让它工作。例如,使用第二个链接I van获得给定国家的最小值,而不是所有国家的最小值。
Hope someone can help me!
希望有人能帮助我!
3 个解决方案
#1
3
You could iterate over all countries and over all keys and check if the value is not equal to null
. Then get min
and max
values.
您可以遍历所有国家和所有键,并检查值是否不等于null。然后得到最小值和最大值。
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);
#2
1
If you are permitted to use ES6:
如果您被允许使用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)
#3
0
I'm assuming you want to check all of the elements of your array for a valid number, and get the min/max of the resulting number set. Here is a code sample to do that. You can click run to see the result.
我假设你想要检查数组的所有元素以得到一个有效的数字,并得到结果数集的最小/最大值。您可以单击run查看结果。
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);
#1
3
You could iterate over all countries and over all keys and check if the value is not equal to null
. Then get min
and max
values.
您可以遍历所有国家和所有键,并检查值是否不等于null。然后得到最小值和最大值。
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);
#2
1
If you are permitted to use ES6:
如果您被允许使用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)
#3
0
I'm assuming you want to check all of the elements of your array for a valid number, and get the min/max of the resulting number set. Here is a code sample to do that. You can click run to see the result.
我假设你想要检查数组的所有元素以得到一个有效的数字,并得到结果数集的最小/最大值。您可以单击run查看结果。
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);