按数组分组

get grouped by array

我有一个 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"
}

请求的结果(使用underscorelodash)是:

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

您可以 reduce 进入由字符串索引的对象,该字符串由 RacenameCategoryGender 组成,由一个字符连接' t 出现在值中,例如 _。例如,您在问题中的输入将生成一个键为 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);