2

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').

my dataset concerns life expectancy for all countries from 1995 till 2016 and looks like this:

[{"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.

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.

Hope someone can help me!

7
  • 4
    please add a wanted result and the code, you tried. Commented Mar 17, 2017 at 13:50
  • 3
    The min and max for a year ? For every year ? By country ? Commented Mar 17, 2017 at 13:51
  • The overall structure of the array makes it difficult to work with it Commented Mar 17, 2017 at 13:52
  • NinaScholz and @Weedoze, is my question clear like this? Commented Mar 17, 2017 at 14:00
  • You could get the min/max of each country, and then the min/max of those. Commented Mar 17, 2017 at 14:00

3 Answers 3

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.

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

Sign up to request clarification or add additional context in comments.

Comments

1

If you are permitted to use 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) 

Comments

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.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.