user19329953
user19329953

Reputation: 63

How to extract all arrays in json object

I have a json object which has a collection of countries. Each country can have multiple regions which are represented in an array. I want to get the regions only and put all regions into one list.However when I map the data it doesn't put all the regions in a list. What am I missing here?

Here is the call to get the json - i am trying to just get the regions:

this.service.getData().subscribe((data: any) => {
      this.list = data.map((c:any) => c.Regions);
    });

the json object:

{

    "Country": "Antarctica",
    "Regions": [
        "Adélie Land",
        "Argentine Antarctica",
        "Australian Antarctic Territory",
        "British Antarctic Territory",
        "Chilean Antarctic Territory",
        "Peter I Island",
        "Queen Maud Land",
        "Ross Dependency"
    ]
},
{
    "Country": "Antigua And Barbuda",
    "Regions": []
},
{
    "Country": "Argentina",
    "Regions": [
        "Buenos Aires",
        "Cordoba",
        "Buenos Aires City",
        "Catamarca",
        "Chaco",
        "Chubut",
        "San Luis",
        "Santa Cruz",
        "Santa Fe",
        "Santiago del Estero",
        "Tierra del Fuego",
        "Tucuman",
        "Mendoza",
        "Misiones",
        "Neuquen",
        "Rio Negro",
        "Salta",
        "San Juan",
        "Corrientes",
        "Entre Rios",
        "Formosa",
        "Jujuy",
        "La Pampa",
        "La Rioja"
    ]
},

Upvotes: 0

Views: 188

Answers (3)

Jeremy
Jeremy

Reputation: 1568

Your data is an object, not an array, just a quick stab at it - so don't kill me if i am wrong - but may be something like this? Would need to iterate over the keys, and in those keys get the Regions.

this.service.getData().subscribe((data: any) => {
   this.list = Object.keys(data.map((c:any) => c.Regions));
});

Updated - use flatMap from lodash, probs what you want.

Upvotes: 0

R4ncid
R4ncid

Reputation: 7129

you can use flatMap for that

like this

const extract = data => data.flatMap(d => d.Regions)

const data = [{

    "Country": "Antarctica",
    "Regions": [
        "Adélie Land",
        "Argentine Antarctica",
        "Australian Antarctic Territory",
        "British Antarctic Territory",
        "Chilean Antarctic Territory",
        "Peter I Island",
        "Queen Maud Land",
        "Ross Dependency"
    ]
},
{
    "Country": "Antigua And Barbuda",
    "Regions": []
},
{
    "Country": "Argentina",
    "Regions": [
        "Buenos Aires",
        "Cordoba",
        "Buenos Aires City",
        "Catamarca",
        "Chaco",
        "Chubut",
        "San Luis",
        "Santa Cruz",
        "Santa Fe",
        "Santiago del Estero",
        "Tierra del Fuego",
        "Tucuman",
        "Mendoza",
        "Misiones",
        "Neuquen",
        "Rio Negro",
        "Salta",
        "San Juan",
        "Corrientes",
        "Entre Rios",
        "Formosa",
        "Jujuy",
        "La Pampa",
        "La Rioja"
    ]
}]

console.log(extract(data))

Upvotes: 3

Robi Szekely
Robi Szekely

Reputation: 11

Here is an example that is working with the data:

const list = []
Object.keys(data).map(key => {
  if (key === "Regions") list.push(...data[key])
}); 
  

console.log(list);
<script>
const data = {
    "Country": "Antarctica",
    "Regions": [
        "Adélie Land",
        "Argentine Antarctica",
        "Australian Antarctic Territory",
        "British Antarctic Territory",
        "Chilean Antarctic Territory",
        "Peter I Island",
        "Queen Maud Land",
        "Ross Dependency"
    ]
}
</script>

Upvotes: 0

Related Questions