Search code examples
javascriptarraysnested-loops

Looping through nested array in javascript


I have a complex array which i need to loop through -

  const arr =   [
  {
    "name": "CLASS",
    "cover": [
      {
        "name": "benefits",
        "interest": [
          {
            "name": "one",
            "limits": [
              {
                "defaultValue": 200
              }
            ]
            },
            {
              "name": "two",
              "limits": [
                {
                  "defaultValue": 300
                }
              ]
            }],
            "limits": [
              {
                "defaultValue": "Y"
              }
            ]
          }
        ]
      }, 
        {
    "name": "Mass",
    "cover": [
      {
        "name": "benefits",
        "interest": [
          {
            "name": "one",
            "limits": [
              {
                "defaultValue": 300
              }
            ]
            },
            {
              "name": "two",
              "limits": [
                {
                  "defaultValue": 500
                }
              ]
            }],
            "limits": [
              {
                "defaultValue": "Y"
              }
            ]
          }
        ]
      }
    ]
with the above array. I need to form a new array like below

  const output =   [
  {
    "name": "benefits",
    "interest": [
      {
        "name": "one",
        "classLimits": 200,
        "massLimits": 300
      },
      {
        "name": "two",
        "classLimits": 300,
        "massLimits": 500
      },
      
    ],
    "classLimit": "Y",
    "massLimit": "Y"
  }
]

I tried few methods to best of my ability but not able to get the desired output.

Any helps on this?

Thanks for your time


Solution

  • You can use array#reduce and create an object accumulator based on name and group the same interest and limit and then extract all values using Object.values().

    const arr = [ { "name": "CLASS", "cover": [ { "name": "benefits", "interest": [ { "name": "one", "limits": [ { "defaultValue": 200 } ] }, { "name": "two", "limits": [ { "defaultValue": 300 } ] }], "limits": [ { "defaultValue": "Y" } ] } ] }, { "name": "Mass", "cover": [ { "name": "benefits", "interest": [ { "name": "one", "limits": [ { "defaultValue": 300 } ] }, { "name": "two", "limits": [ { "defaultValue": 500 } ] }], "limits": [ { "defaultValue": "Y" } ] } ] } ],
        result = arr.reduce((r, o) => {
          const prefix = o.name.toLowerCase();
          o.cover.forEach(({name, interest, limits}) => {
            r[name] = r[name] || {name, interest: {}};
            interest.forEach(int => {
              r[name].interest[int.name] = r[name].interest[int.name] || { name: int.name};
              r[name].interest[int.name][`${prefix}Limits`] = int.limits[0].defaultValue;
            })
            r[name][`${prefix}Limit`] = r[name][`${prefix}Limit`] || limits[0].defaultValue;
          })
          return r;
        },{}),
        output = Object.values(result).map(val => ({...val, interest: Object.values(val.interest)}));
    console.log(output);