Help understanding how to extract a set of data from JSON

Hi, I need help figuring out how to iterate through an array of objects.

Below is my original json data:

[
	{
		"1960": 54211,
		"1961": 55438,
		"1962": 56225,
		...
		"2019": 106314,
		"CountryName": "Aruba",
		"CountryCode": "ABW"
	},
	{
		"1960": 8996973,
		"1961": 9169410,
		"1962": 9351441,
		...
		"2019": 38041754,
		"CountryName": "Afghanistan",
		"CountryCode": "AFG"
	},
	{
		"1960": 5454933,
		"1961": 5531472,
		"1962": 5608539,
		...
		"2019": 31825295,
		"CountryName": "Angola",
		"CountryCode": "AGO"
	},

	...

	{
		"1960": 92418,
		"1961": 100796,
		"1962": 112118,
		...
		"2019": 9770529,
		"CountryName": "Zimbabwe",
		"CountryCode": "ZMB"
	}
]

I want to know how to group date given a couple of parameters for example:

  • An array of CountryNames eg: ["Aruba", "Angola", "USA"]
  • and also a range of Min and Max Years eg: [1968, 2000]

So how can I create sub set object from that information?

Any help or pointers will be much appreciated.

Ok I manage to get extract the years within the range and for the selected countries by doing this

plot_multiple_years = () => {
  let result = [];
  // loop trough the selected countries
  country_list.forEach(country => {
    // look for the selectec countries in the entire data set
    wpd.forEach(d => {
      // if there is a match
      if (d['CountryName'] === country) {
        let years_in_range = [];
        // then get the years within the range
        Object.keys(d).map(y => {
          if (y >= year_range[0] && y <= year_range[1])
            years_in_range.push(d[y]);
        });
        // result.push(years_in_range);
        result.push({
          ['country']: country,
          ['years']: years_in_range
        });
      }
    });
  });
  return result;
}

But the algorithm is Oⁿ I must refactor this to make it more efficient, suggestions are welcome!

Is there any benefit to tidying the data once and then using simpler filters?

Thank you for the article I’ll check it out

The notebook I made has examples for your JSON, too. :slight_smile: