Home > Enterprise >  Group an array of nested objects by 'sector' keyword - Javascript
Group an array of nested objects by 'sector' keyword - Javascript

Time:04-20

I have an object where the value is an array of objects. I want to go through the object and categorize it by sector. This is my data.

{
   "Word 1":[
      {
         "name":"Walmart",
         "id":"Walmart",
         "no":1,
         "sector":"Retailing",
         "url":"https://www.walmart.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":2
            },
            {
               "year":2019,
               "totalCount":1
            },
            {
               "year":2020,
               "totalCount":1
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"Amazon",
         "id":"amazon",
         "no":2,
         "sector":"Retailing",
         "url":"https://www.amazon.com/",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":5
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"Apple",
         "id":"Apple",
         "no":3,
         "sector":"Technology",
         "url":"https://www.apple.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":0
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"CVS Health",
         "id":"CVSHealth",
         "no":4,
         "sector":"Health Care",
         "url":"https://www.cvshealth.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":1
            },
            {
               "year":2020,
               "totalCount":0
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      }
   ],
   "Word 2":[
      {
         "name":"Walmart",
         "id":"Walmart",
         "no":1,
         "sector":"Retailing",
         "url":"https://www.walmart.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":1
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"Amazon",
         "id":"amazon",
         "no":2,
         "sector":"Retailing",
         "url":"https://www.amazon.com/",
         "data":[
            {
               "year":2017,
               "totalCount":117
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":0
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"Apple",
         "id":"Apple",
         "no":3,
         "sector":"Technology",
         "url":"https://www.apple.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":3
            },
            {
               "year":2019,
               "totalCount":5
            },
            {
               "year":2020,
               "totalCount":3
            },
            {
               "year":2021,
               "totalCount":9
            }
         ]
      },
      {
         "name":"CVS Health",
         "id":"CVSHealth",
         "no":4,
         "sector":"Health Care",
         "url":"https://www.cvshealth.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":0
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      }
   ],
   "Word 3":[
      {
         "name":"Walmart",
         "id":"Walmart",
         "no":1,
         "sector":"Retailing",
         "url":"https://www.walmart.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":0
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"Amazon",
         "id":"amazon",
         "no":2,
         "sector":"Retailing",
         "url":"https://www.amazon.com/",
         "data":[
            {
               "year":2017,
               "totalCount":42
            },
            {
               "year":2018,
               "totalCount":0
            },
            {
               "year":2019,
               "totalCount":0
            },
            {
               "year":2020,
               "totalCount":0
            },
            {
               "year":2021,
               "totalCount":0
            }
         ]
      },
      {
         "name":"Apple",
         "id":"Apple",
         "no":3,
         "sector":"Technology",
         "url":"https://www.apple.com",
         "data":[
            {
               "year":2017,
               "totalCount":0
            },
            {
               "year":2018,
               "totalCount":3
            },
            {
               "year":2019,
               "totalCount":12
            },
            {
               "year":2020,
               "totalCount":3
            },
            {
               "year":2021,
               "totalCount":3
            }
         ]
      },
      {
         "name":"CVS Health",
         "id":"CVSHealth",
         "no":4,
         "sector":"Health Care",
         "url":"https://www.cvshealth.com",
         "data":[
            {
               "year":2017,
               "totalCount":13
            },
            {
               "year":2018,
               "totalCount":25
            },
            {
               "year":2019,
               "totalCount":26
            },
            {
               "year":2020,
               "totalCount":7
            },
            {
               "year":2021,
               "totalCount":3
            }
         ]
      }
   ]
}

Here the data is categorized by a keyword. However, I want to categorize it by sector and get the sum of totalCount for each sector.

This is what I would like to achieve.

{
   "Retailing":{
      "Word 1":9,
      "Word 2":118,
      "Word 3":42
   },
   "Technology":{
      "Word 1":0,
      "Word 2":20,
      "Word 3":21
   },
   "Health Care":{
      "Word 1":1,
      "Word 2":0,
      "Word 3":74
   }
}

This is what I have done so far.

const objectData = {
  "Word 1": [{
      "name": "Walmart",
      "id": "Walmart",
      "no": 1,
      "sector": "Retailing",
      "url": "https://www.walmart.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 2
        },
        {
          "year": 2019,
          "totalCount": 1
        },
        {
          "year": 2020,
          "totalCount": 1
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "Amazon",
      "id": "amazon",
      "no": 2,
      "sector": "Retailing",
      "url": "https://www.amazon.com/",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 5
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "Apple",
      "id": "Apple",
      "no": 3,
      "sector": "Technology",
      "url": "https://www.apple.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 0
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "CVS Health",
      "id": "CVSHealth",
      "no": 4,
      "sector": "Health Care",
      "url": "https://www.cvshealth.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 1
        },
        {
          "year": 2020,
          "totalCount": 0
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    }
  ],
  "Word 2": [{
      "name": "Walmart",
      "id": "Walmart",
      "no": 1,
      "sector": "Retailing",
      "url": "https://www.walmart.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 1
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "Amazon",
      "id": "amazon",
      "no": 2,
      "sector": "Retailing",
      "url": "https://www.amazon.com/",
      "data": [{
          "year": 2017,
          "totalCount": 117
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 0
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "Apple",
      "id": "Apple",
      "no": 3,
      "sector": "Technology",
      "url": "https://www.apple.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 3
        },
        {
          "year": 2019,
          "totalCount": 5
        },
        {
          "year": 2020,
          "totalCount": 3
        },
        {
          "year": 2021,
          "totalCount": 9
        }
      ]
    },
    {
      "name": "CVS Health",
      "id": "CVSHealth",
      "no": 4,
      "sector": "Health Care",
      "url": "https://www.cvshealth.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 0
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    }
  ],
  "Word 3": [{
      "name": "Walmart",
      "id": "Walmart",
      "no": 1,
      "sector": "Retailing",
      "url": "https://www.walmart.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 0
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "Amazon",
      "id": "amazon",
      "no": 2,
      "sector": "Retailing",
      "url": "https://www.amazon.com/",
      "data": [{
          "year": 2017,
          "totalCount": 42
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 0
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "Apple",
      "id": "Apple",
      "no": 3,
      "sector": "Technology",
      "url": "https://www.apple.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 3
        },
        {
          "year": 2019,
          "totalCount": 12
        },
        {
          "year": 2020,
          "totalCount": 3
        },
        {
          "year": 2021,
          "totalCount": 3
        }
      ]
    },
    {
      "name": "CVS Health",
      "id": "CVSHealth",
      "no": 4,
      "sector": "Health Care",
      "url": "https://www.cvshealth.com",
      "data": [{
          "year": 2017,
          "totalCount": 13
        },
        {
          "year": 2018,
          "totalCount": 25
        },
        {
          "year": 2019,
          "totalCount": 26
        },
        {
          "year": 2020,
          "totalCount": 7
        },
        {
          "year": 2021,
          "totalCount": 3
        }
      ]
    }
  ]
}

const result = _.groupBy(_.map(_.flattenDeep(_.map(_.map(_.map(_.keys(objectData), key => {
  return {
    data: objectData[key],
    word: key
  }
}), i => {
  return {
    data: i.data.map(j => {
      return {
        ...j,
        word: i.word
      }
    })
  }
}), 'data')), k => {
  return {
    sector: k.sector,
    word: k.word,
    count: _.sumBy(k.data, 'totalCount')
  }
}), 'sector')


console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

CodePudding user response:

Object entries, forEach over the array, and reduce to get the total.

var foo = {
  "Word 1": [{
      "name": "Walmart",
      "id": "Walmart",
      "no": 1,
      "sector": "Retailing",
      "url": "https://www.walmart.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 2
        },
        {
          "year": 2019,
          "totalCount": 1
        },
        {
          "year": 2020,
          "totalCount": 1
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "Amazon",
      "id": "amazon",
      "no": 2,
      "sector": "Retailing",
      "url": "https://www.amazon.com/",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 5
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "Apple",
      "id": "Apple",
      "no": 3,
      "sector": "Technology",
      "url": "https://www.apple.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 0
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "CVS Health",
      "id": "CVSHealth",
      "no": 4,
      "sector": "Health Care",
      "url": "https://www.cvshealth.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 1
        },
        {
          "year": 2020,
          "totalCount": 0
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    }
  ],
  "Word 2": [{
      "name": "Walmart",
      "id": "Walmart",
      "no": 1,
      "sector": "Retailing",
      "url": "https://www.walmart.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 1
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "Amazon",
      "id": "amazon",
      "no": 2,
      "sector": "Retailing",
      "url": "https://www.amazon.com/",
      "data": [{
          "year": 2017,
          "totalCount": 117
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 0
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "Apple",
      "id": "Apple",
      "no": 3,
      "sector": "Technology",
      "url": "https://www.apple.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 3
        },
        {
          "year": 2019,
          "totalCount": 5
        },
        {
          "year": 2020,
          "totalCount": 3
        },
        {
          "year": 2021,
          "totalCount": 9
        }
      ]
    },
    {
      "name": "CVS Health",
      "id": "CVSHealth",
      "no": 4,
      "sector": "Health Care",
      "url": "https://www.cvshealth.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 0
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    }
  ],
  "Word 3": [{
      "name": "Walmart",
      "id": "Walmart",
      "no": 1,
      "sector": "Retailing",
      "url": "https://www.walmart.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 0
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "Amazon",
      "id": "amazon",
      "no": 2,
      "sector": "Retailing",
      "url": "https://www.amazon.com/",
      "data": [{
          "year": 2017,
          "totalCount": 42
        },
        {
          "year": 2018,
          "totalCount": 0
        },
        {
          "year": 2019,
          "totalCount": 0
        },
        {
          "year": 2020,
          "totalCount": 0
        },
        {
          "year": 2021,
          "totalCount": 0
        }
      ]
    },
    {
      "name": "Apple",
      "id": "Apple",
      "no": 3,
      "sector": "Technology",
      "url": "https://www.apple.com",
      "data": [{
          "year": 2017,
          "totalCount": 0
        },
        {
          "year": 2018,
          "totalCount": 3
        },
        {
          "year": 2019,
          "totalCount": 12
        },
        {
          "year": 2020,
          "totalCount": 3
        },
        {
          "year": 2021,
          "totalCount": 3
        }
      ]
    },
    {
      "name": "CVS Health",
      "id": "CVSHealth",
      "no": 4,
      "sector": "Health Care",
      "url": "https://www.cvshealth.com",
      "data": [{
          "year": 2017,
          "totalCount": 13
        },
        {
          "year": 2018,
          "totalCount": 25
        },
        {
          "year": 2019,
          "totalCount": 26
        },
        {
          "year": 2020,
          "totalCount": 7
        },
        {
          "year": 2021,
          "totalCount": 3
        }
      ]
    }
  ]
};

// Loop over the entries in the object
const result = Object.entries(foo).reduce((obj, [key, arr]) => {
  // Each word in the object has an array so we need to loop over it
  arr.forEach(value => {
    // have we seen this sector yet?
    obj[value.sector] = obj[value.sector] || {};
    // get the total for this sector's key
    obj[value.sector][key] = (obj[value.sector][key] || 0)   value.data.reduce((total, {
      totalCount
    }) => total   totalCount, 0);
  });
  return obj;
}, {});

console.log(result);

CodePudding user response:

This should get you started (please note you probably should create a separate function to loop over your data, have it return the sum and call it in the individual cases).

objectData.forEach(function(word){
 var a = 0;
 var b = 0;
 // Etc.

  switch(word.sector){
    case "Retailing":
      // Do something;
      break;
    case "Health Care":
      // Do something;
      break;
    default:
      // Do something;
  }

  // Do something with your results
});
  • Related