# Help understanding how to extract a set of data from JSON

**URL:** <https://talk.observablehq.com/t/help-understanding-how-to-extract-a-set-of-data-from-json/3919>\
**Category:** Help\
**Created:** [September 23, 2020, 11:14am UTC](https://talk.observablehq.com/t/help-understanding-how-to-extract-a-set-of-data-from-json/3919 "2020-09-23T11:14:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nardove](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/nardove/32/3617_2.png) [@nardove](https://talk.observablehq.com/u/nardove)\
**Post date:** [September 23, 2020, 11:14am UTC](https://talk.observablehq.com/t/help-understanding-how-to-extract-a-set-of-data-from-json/3919/1 "2020-09-23T11:14:25Z")

</div>

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

Below is my original **json** data:

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

---

<div class="post-metadata">

**Author:** ![nardove](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/nardove/32/3617_2.png) [@nardove](https://talk.observablehq.com/u/nardove)\
**Post date:** [September 23, 2020, 6:13pm UTC](https://talk.observablehq.com/t/help-understanding-how-to-extract-a-set-of-data-from-json/3919/2 "2020-09-23T18:13:33Z")

</div>

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

```javascript
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!

---

<div class="post-metadata">

**Author:** ![kpm-at-hfi](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/kpm-at-hfi/32/4155_2.png) [@kpm-at-hfi](https://talk.observablehq.com/u/kpm-at-hfi)\
**Post date:** [September 23, 2020, 11:56pm UTC](https://talk.observablehq.com/t/help-understanding-how-to-extract-a-set-of-data-from-json/3919/3 "2020-09-23T23:56:36Z")

</div>

Is there any benefit to [tidying](https://vita.had.co.nz/papers/tidy-data.pdf) the data once and then using simpler filters?

> **[Tidying and filtering data](https://observablehq.com/@kpm-at-hfi/tidying-and-filtering-data)**
>
> An Observable notebook by kpm-at-hfi.

---

<div class="post-metadata">

**Author:** ![nardove](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/nardove/32/3617_2.png) [@nardove](https://talk.observablehq.com/u/nardove)\
**Post date:** [September 24, 2020, 5:57am UTC](https://talk.observablehq.com/t/help-understanding-how-to-extract-a-set-of-data-from-json/3919/4 "2020-09-24T05:57:17Z")

</div>

Thank you for the article I’ll check it out

---

<div class="post-metadata">

**Author:** ![kpm-at-hfi](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/kpm-at-hfi/32/4155_2.png) [@kpm-at-hfi](https://talk.observablehq.com/u/kpm-at-hfi)\
**Post date:** [September 24, 2020, 2:11pm UTC](https://talk.observablehq.com/t/help-understanding-how-to-extract-a-set-of-data-from-json/3919/5 "2020-09-24T14:11:50Z")

</div>

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