【问题标题】:group objects in array based on value of key in object根据对象中键的值对数组中的对象进行分组
【发布时间】:2018-11-23 02:55:06
【问题描述】:

我想根据日期对以下数据进行排序 - 不包括时间戳。

注意:我可以访问 moment 来完成这项任务。

我的数据如下所示:

const data = [
   {
     "fixture": "AC v Inter",
     "kickOffTime": "2018-06-14T15:00:00Z",
   },
   {
     "fixture": "DC v NYC",
     "kickOffTime": "2018-06-15T12:00:00Z",
   },
   {
     "fixture": "AFC v LPC",
     "kickOffTime": "2018-06-15T15:00:00Z",
   },
   {
      "fixture": "DTA v MC",
      "kickOffTime": "2018-06-15T18:00:00Z",
    },
    {
       "fixture": "LAC v GC",
       "kickOffTime": "2018-06-16T18:00:00Z",
    }
];

我尝试了多种方法。我希望达到的最终结果是以下数据结构。

const updatedDataStructure = [
   {
     date: "2018-06-14",
     fixtures: [{
        "fixture": "AC v Inter",
        "kickOffTime": "2018-06-14T15:00:00Z",
      }]
   },
   {
     date: "2018-06-15",
     fixtures: [
      {
        "fixture": "DC v NYC",
        "kickOffTime": "2018-06-15T12:00:00Z",
       }, 
      {
        "fixture": "AFC v LPC",
       "kickOffTime": "2018-06-15T15:00:00Z",
      },
      {
        "fixture": "DTA v MC",
        "kickOffTime": "2018-06-15T18:00:00Z",
       },
     ]
   }, 
   {
     date: "2018-06-16",
     fixtures: [{
         "fixture": "LAC v GC",
         "kickOffTime": "2018-06-16T18:00:00Z",
     }]
   },
];

这是我最近的尝试,几乎奏效了:

const result = fixtures.reduce(function (r, a) {
  r[moment(a.kickOffTime).format('ddd Do MMM')] = r[moment(a.kickOffTime).format('ddd Do MMM')] || [];
  r[moment(a.kickOffTime).format('ddd Do MMM')].push(a);
  return r;
}, Object.create(null));

【问题讨论】:

    标签: javascript ecmascript-6 reduce


    【解决方案1】:

    您可以只从日期中获取一个切片,然后获取对象的条目并映射新的键/值。

     const
         data = [{ fixture: "AC v Inter", kickOffTime: "2018-06-14T15:00:00Z" }, { fixture: "DC v NYC", kickOffTime: "2018-06-15T12:00:00Z" }, { fixture: "AFC v LPC", kickOffTime: "2018-06-15T15:00:00Z" }, { fixture: "DTA v MC", kickOffTime: "2018-06-15T18:00:00Z" }, { fixture: "LAC v GC", kickOffTime: "2018-06-16T18:00:00Z" }];
         result = Object
            .entries(data.reduce((r, a) => {
                var key = a.kickOffTime.slice(0, 10);
                r[key] = r[key] || [];
                r[key].push(a);
                return r;
            }, Object.create(null)))
            .map(([date, fixtures]) => ({ date, fixtures }));
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案2】:

      您可以使用reduce 将数组分组到一个对象中。使用Object.values 可以将对象转换为数组。

      const data = [{
          "fixture": "AC v Inter",
          "kickOffTime": "2018-06-14T15:00:00Z",
        },
        {
          "fixture": "DC v NYC",
          "kickOffTime": "2018-06-15T12:00:00Z",
        },
        {
          "fixture": "AFC v LPC",
          "kickOffTime": "2018-06-15T15:00:00Z",
        },
        {
          "fixture": "DTA v MC",
          "kickOffTime": "2018-06-15T18:00:00Z",
        },
        {
          "fixture": "LAC v GC",
          "kickOffTime": "2018-06-16T18:00:00Z",
        }
      ];
      
      const result = Object.values(data.reduce((c, v) => {
        let t = v['kickOffTime'].split('T', 1)[0];
        c[t] = c[t] || {date: t,fixtures: []}
        c[t].fixtures.push(v);
        return c;
      }, {}));
      
      console.log(result);

      【讨论】:

      • 其他答案先到了,所以我会把它给其他用户,但也给了你一个 +1,很好的答案!
      • @peterflanagan 不用担心。谢谢:)
      • 我实际上也在使用您的解决方案 - 它有点整洁:-D
      猜你喜欢
      • 2021-04-12
      • 2023-02-05
      • 2021-09-25
      • 1970-01-01
      • 2021-09-12
      • 2019-03-23
      • 1970-01-01
      • 2013-11-25
      • 2017-09-29
      相关资源
      最近更新 更多