按 'sector' 关键字对嵌套对象数组进行分组 - Javascript

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

我有一个对象,它的值是一个对象数组。我想遍历对象并按扇区对其进行分类。这是我的数据。

{
   "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
            }
         ]
      }
   ]
}

此处数据按关键字分类。但是,我想按扇区分类,得到每个扇区的totalCount之和。

这就是我想要实现的。

{
   "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
   }
}

这是我到目前为止所做的。

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>

这应该让你开始(请注意你可能应该创建一个单独的函数来循环你的数据,让它 return 总和并在个别情况下调用它)。

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
});

对象条目,forEach 遍历数组,并减少以获得总数。

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);