代码之家  ›  专栏  ›  技术社区  ›  JumpIntoTheWater

按数组分组

  •  1
  • JumpIntoTheWater  · 技术社区  · 7 年前

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

    请求的结果(使用 underscore 或 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"
         }]
    1 回复  |  直到 7 年前
        1
  •  1
  •   CertainPerformance    7 年前

    你可以 reduce 一个由字符串索引的对象,由 Racename , Category 和 Gender ,由值中不存在的字符联接,例如 _ . 例如,您在问题中的输入将导致一个键为的对象 10KM_34_Male . 在每次迭代中,检查构造的键是否存在-如果不存在,则使用空键创建对象 runner 数组。然后,推到 跑步者 数组。

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

    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);
    推荐文章