-8

Take out particular array out of nested object in javascript I have a nested object:

INPUT:

 {
  "countryList": [
    {
      "label": "Australia",
      "id": "555",
      "productHierarchy": "ABC",
      "locked": "true",
      "products": [
        {
          "id": "342",
          "label": "342- Test",
          "description": "Test"
        },
        {
          "id": "123456",
          "label": "123456- Test",
          "description": "Test"
        }
      ]
    },
    {
      "label": "Brazil",
      "id": "888",
      "productHierarchy": "XYZ",
      "locked": "true",
      "products": [
        {
          "id": "987",
          "label": "987- Test",
          "description": "Test"
        },
        {
          "id": "567",
          "label": "567- Test",
          "description": "Test"
        }
      ]
    }
  ]

}

From this object I want an array of products according to countryList label.

OUTPUT

If I give country label as Australia

I want:

products= [
        {
          "id": "342",
          "label": "342- Test",
          "description": "Test"
        },
        {
          "id": "123456",
          "label": "123456- Test",
          "description": "Test"
        }
      ]

Please help me out solving this.

Sheena
  • 21
  • 4
  • Possible duplicate of [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – Sebastian Simon Apr 04 '18 at 07:56

2 Answers2

1

You can use Array#find :

label => data.countryList.find(e => e.label == label).products;

const data = {
  "countryList": [
    {
      "label": "Australia",
      "id": "555",
      "productHierarchy": "ABC",
      "locked": "true",
      "products": [
        {
          "id": "342",
          "label": "342- Test",
          "description": "Test"
        },
        {
          "id": "123456",
          "label": "123456- Test",
          "description": "Test"
        }
      ]
    },
    {
      "label": "Brazil",
      "id": "888",
      "productHierarchy": "XYZ",
      "locked": "true",
      "products": [
        {
          "id": "987",
          "label": "987- Test",
          "description": "Test"
        },
        {
          "id": "567",
          "label": "567- Test",
          "description": "Test"
        }
      ]
    }
  ]

};

const getProducts = label => data.countryList.find(e => e.label == label).products;

console.log(getProducts('Brazil'));
Zenoo
  • 12,670
  • 4
  • 45
  • 69
0

One of the way is to use filter of Array.prototype(like below)

x={countryList:[{label:"Australia",id:"555",productHierarchy:"ABC",locked:"true",products:[{id:"342",label:"342- Test",description:"Test"},{id:"123456",label:"123456- Test",description:"Test"}]},{label:"Brazil",id:"888",productHierarchy:"XYZ",locked:"true",products:[{id:"987",label:"987- Test",description:"Test"},{id:"567",label:"567- Test",description:"Test"}]}]};

function getProducts(countryName){
  return x.countryList.filter(e=>e.label=countryName)[0].products;
}

console.log(getProducts("Australia"));
yajiv
  • 2,901
  • 2
  • 15
  • 25