javascript - 按数组分组

标签 javascript angular typescript underscore.js

我有一个 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 组成code>,由一个不会出现在值中的字符连接,例如 _。例如,您在问题中的输入将生成一个键为 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);

关于javascript - 按数组分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54060494/

相关文章:

javascript - 在 angular2 中导出 xlsx 文件( typescript )

Angular 单元测试 - 如何窥探可观察的属性

angular - 从docker ui容器调用到api容器

typescript - Webpack "Cannot find module"为 "module": "esnext"

javascript - 包含在 for 循环中困惑的 Promise 的对象数组

javascript - 添加日期结果不正确

javascript - 如何使用 dojo 对 Json 中的不同值进行计数或求和

javascript - 为什么我的最小和最大日期设置在我的日期选择器中不起作用?

typescript - TS - 无法从子级调用 super() 内的公共(public)方法

angular - 如何调试 angular2 typescript 文件