group objects in array based on value of key in object

回眸只為那壹抹淺笑 提交于 2019-12-18 07:05:13

问题


I have the following data that I want to sort based on the date - not including the timestamp.

NOTE: I have access to moment for this task.

My data looks like the following:

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",
    }
];

I have tried a number of approaches. The final result I am hoping to achieve is the following data structure.

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",
     }]
   },
];

Here was my latest attempt which NEARLY worked:

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));

回答1:


You could take just a slice from the date and later take the entries of the object and map new key/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" }];
     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:


You can group the array using reduce into an object. Using Object.values you can convert the object into array.

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);


来源:https://stackoverflow.com/questions/50838686/group-objects-in-array-based-on-value-of-key-in-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!