首页 > 解决方案 > 按数组分组

问题描述

我有一个 JSON 文件,我希望根据其中的三个字段对这个 JSON 进行分组。

JSON 如下所示(当然还有更多项目):

{
  "Racename": "10KM",
  "Category": 34,
  "Gender": "Male",
  "Work": "Google",
  "FullName": "Dave Happner",
  "Rank": 1,
  "Ponit": 1,
  "Numparticipant": 0,
  "rankparticipant": 0,
  "precentagePart": "0",
  "NumRaces": 1,
  "RaceTime": "2018-10-18T00:34:20",
  "rankCat": 1,
  "PointCat": 1,
  "RaceDate": "2018-10-05"
}

请求的结果(使用underscoreor lodash)是:

 [
   {
     "Racename" : "10KM",
     "Category": "34",
     "Gender": "Male",
     runner : [
     {
      "Work": "Google",
      "FullName": "Dave Happner",
      "Rank": 1,
      "Ponit": 1,
      "Numparticipant": 0,
      "rankparticipant": 0,
      "precentagePart": "0",
      "NumRaces": 1,
      "RaceTime": "2018-10-18T00:34:20",
      "rankCat": 1,
      "PointCat": 1,
      "RaceDate": "2018-10-05"
     }]

标签: javascriptangulartypescriptunderscore.js

解决方案


您可以reduce进入由字符串索引的对象,该字符串由 、 和 组成RacenameCategory并由Gender值中不存在的字符连接,例如_. 例如,您在问题中的输入将产生一个键为 的对象10KM_34_Male。在每次迭代中,检查构造的键是否存在 - 如果不存在,则使用空runner数组创建对象。然后,推送到runner数组。

完成后reduce,您可以获取对象的值以获得所需的数组输出:

const input = [{
  "Racename": "10KM",
  "Category": 34,
  "Gender": "Male",
  "Work": "Google",
  "FullName": "Dave Happner",
  "Rank": 1,
  "Ponit": 1,
  "Numparticipant": 0,
  "rankparticipant": 0,
  "precentagePart": "0",
  "NumRaces": 1,
  "RaceTime": "2018-10-18T00:34:20",
  "rankCat": 1,
  "PointCat": 1,
  "RaceDate": "2018-10-05"
}];

const outputObj = input.reduce((a, { Racename, Category, Gender, ...rest }) => {
  const key = [Racename, Category, Gender].join('_');
  if (!a[key]) {
    a[key] = { Racename, Category, Gender, runner: [] };
  }
  a[key].runner.push(rest);
  return a;
}, {});
const output = Object.values(outputObj);
console.log(output);

或者,使用更大的输入:

const input = [{
  "Racename": "10KM",
  "Category": 34,
  "Gender": "Male",
  "Work": "Google",
  "FullName": "Dave Happner",
  "Rank": 1,
  "Ponit": 1,
  "Numparticipant": 0,
  "rankparticipant": 0,
  "precentagePart": "0",
  "NumRaces": 1,
  "RaceTime": "2018-10-18T00:34:20",
  "rankCat": 1,
  "PointCat": 1,
  "RaceDate": "2018-10-05"
},{
  "Racename": "10KM",
  "Category": 34,
  "Gender": "Male",
  "Work": "Amazon",
  "FullName": "Bob Joe",
  "Rank": 12,
  "Ponit": 2,
  "Numparticipant": 0,
  "rankparticipant": 0,
  "precentagePart": "0",
  "NumRaces": 1,
  "RaceTime": "2018-10-18T00:34:20",
  "rankCat": 1,
  "PointCat": 1,
  "RaceDate": "2018-10-05"
},{
  "Racename": "20KM",
  "Category": 40,
  "Gender": "Male",
  "Work": "Google",
  "FullName": "Dave Happner",
  "Rank": 1,
  "Ponit": 1,
  "Numparticipant": 0,
  "rankparticipant": 0,
  "precentagePart": "0",
  "NumRaces": 1,
  "RaceTime": "2018-10-18T00:34:20",
  "rankCat": 1,
  "PointCat": 1,
  "RaceDate": "2018-10-05"
}
];

const outputObj = input.reduce((a, { Racename, Category, Gender, ...rest }) => {
  const key = [Racename, Category, Gender].join('_');
  if (!a[key]) {
    a[key] = { Racename, Category, Gender, runner: [] };
  }
  a[key].runner.push(rest);
  return a;
}, {});
const output = Object.values(outputObj);
console.log(output);


推荐阅读