如何按日期对数组中的项目进行分组?

时间:2017-10-18 03:44:40

标签: javascript arrays for-loop iteration grouping

给出以下对象数组:

[
{
"notes": "Game was played",
"time": "2017-10-04T20:24:30+00:00",
"sport": "hockey",
"owner": "steve",
"players": "10",
"game_id": 1,
},
{
"notes": "Game was played",
"time": "2017-10-04T12:35:30+00:00",
"sport": "lacrosse",
"owner": "steve",
"players": "6",
"game_id": 2,
},
{
"notes": "Game was played",
"time": "2017-10-14T20:32:30+00:00",
"sport": "hockey",
"owner": "steve",
"players": "4",
"game_id": 3,
},
{
"notes": "Game was played",
"time": "2017-10-04T10:12:30+00:00",
"sport": "hockey",
"owner": "henry",
"players": "10",
"game_id": 4,
},
{
"notes": "Game was played",
"time": "2017-10-14T20:34:30+00:00",
"sport": "soccer",
"owner": "john",
"players": "12",
"game_id": 5,
}
]

我正在尝试创建一个新数组,它会创建一个对象,每个日期(作为一个键)以及在该日期播放的任何游戏都列在同一对象中的游戏(另一个键)下。

新数组应如下所示:

[
{
date:'date string',
games:[array of games played on the date]
}, 
{
date: 'other date',
games:[games under the other date]
}
]...

这就是我所做的:

let t = this.state.data; (data above)
        let list = [];
        for (let i = 0; i < t.length; i++) {
            let dates = t[i].time.slice(0,10);

            if (!list[dates]) {
                list[dates] = [];
            }
            list[dates].push(t[i]);
        }

我的版本返回一个数组,其中日期为值,然后在列出的游戏内部,但我试图将它们列在与不同键相同的对象中。

非常感谢任何帮助或指导。

4 个答案:

答案 0 :(得分:10)

这是使用reduce的解决方案。将time拆分为日期字符串,然后为每个日期设置一个键。如果密钥存在,则将其推送到数组:

更新添加了数组格式版本

&#13;
&#13;
const data = [
  {notes: 'Game was played', time: '2017-10-04T20:24:30+00:00', sport: 'hockey', owner: 'steve', players: '10', game_id: 1},
  { notes: 'Game was played', time: '2017-10-04T12:35:30+00:00', sport: 'lacrosse', owner: 'steve', players: '6', game_id: 2 },
  { notes: 'Game was played', time: '2017-10-14T20:32:30+00:00', sport: 'hockey', owner: 'steve', players: '4', game_id: 3 },
  { notes: 'Game was played', time: '2017-10-04T10:12:30+00:00', sport: 'hockey', owner: 'henry', players: '10', game_id: 4 },
  { notes: 'Game was played', time: '2017-10-14T20:34:30+00:00', sport: 'soccer', owner: 'john', players: '12', game_id: 5 }
];

// this gives an object with dates as keys
const groups = data.reduce((groups, game) => {
  const date = game.time.split('T')[0];
  if (!groups[date]) {
    groups[date] = [];
  }
  groups[date].push(game);
  return groups;
}, {});

// Edit: to add it in the array format instead
const groupArrays = Object.keys(groups).map((date) => {
  return {
    date,
    games: groups[date]
  };
});

console.log(groupArrays);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是你正在寻找的???

&#13;
&#13;
var data = [
    {
        notes: 'Game was played',
        time: '2017-10-04T20:24:30+00:00',
        sport: 'hockey',
        owner: 'steve',
        players: '10',
        game_id: 1
    },
    {
        notes: 'Game was played',
        time: '2017-10-04T12:35:30+00:00',
        sport: 'lacrosse',
        owner: 'steve',
        players: '6',
        game_id: 2
    },
    {
        notes: 'Game was played',
        time: '2017-10-14T20:32:30+00:00',
        sport: 'hockey',
        owner: 'steve',
        players: '4',
        game_id: 3
    },
    {
        notes: 'Game was played',
        time: '2017-10-04T10:12:30+00:00',
        sport: 'hockey',
        owner: 'henry',
        players: '10',
        game_id: 4
    },
    {
        notes: 'Game was played',
        time: '2017-10-14T20:34:30+00:00',
        sport: 'soccer',
        owner: 'john',
        players: '12',
        game_id: 5
    }
];

function extract() {
    var groups = {};

    data.forEach(function(val) {
        var date = val.time.split('T')[0];
        if (date in groups) {
            groups[date].push(val.sport);
        } else {
            groups[date] = new Array(val.sport);
        }
    });

    console.log(groups);
    return groups;
}

extract();
&#13;
&#13;
&#13;

答案 2 :(得分:0)

    let a =[{
      "notes": "Game was played",
      "time": "2017-10-04T20:24:30+00:00",
      "sport": "hockey",
      "owner": "steve",
      "players": "10",
      "game_id": 1,
      },
      {
      "notes": "Game was played",
      "time": "2017-10-04T12:35:30+00:00",
      "sport": "lacrosse",
      "owner": "steve",
      "players": "6",
      "game_id": 2,
      },
      {
      "notes": "Game was played",
      "time": "2017-10-14T20:32:30+00:00",
      "sport": "hockey",
      "owner": "steve",
      "players": "4",
      "game_id": 3,
      },
      {
      "notes": "Game was played",
      "time": "2017-10-04T10:12:30+00:00",
      "sport": "hockey",
      "owner": "henry",
      "players": "10",
      "game_id": 4,
      },
      {
      "notes": "Game was played",
      "time": "2017-10-14T20:34:30+00:00",
      "sport": "soccer",
      "owner": "john",
      "players": "12",
      "game_id": 5,
      }]

      let finalObj = {}
      a.forEach((games) => {
        const date = games.time.split('T')[0]
        if (finalObj[date]) {
          finalObj[date].push(games);
        } else {
          finalObj[date] = [games];
        }
      })
      console.log(finalObj)

答案 3 :(得分:0)

使用 RxJS, 就我而言,使用 Angular

import { of } from "rxjs";
import { groupBy, map, mergeMap, reduce, toArray } from "rxjs/internal/operators";

const data = [
  {
    "notes": "Game was played",
    "time": "2017-10-04T20:24:30+00:00",
    "sport": "hockey",
    "owner": "steve",
    "players": "10",
    "game_id": 1,
  },
  {
    "notes": "Game was played",
    "time": "2017-10-04T12:35:30+00:00",
    "sport": "lacrosse",
    "owner": "steve",
    "players": "6",
    "game_id": 2,
  },
  {
    "notes": "Game was played",
    "time": "2017-10-14T20:32:30+00:00",
    "sport": "hockey",
    "owner": "steve",
    "players": "4",
    "game_id": 3,
  },
  {
    "notes": "Game was played",
    "time": "2017-10-04T10:12:30+00:00",
    "sport": "hockey",
    "owner": "henry",
    "players": "10",
    "game_id": 4,
  },
  {
    "notes": "Game was played",
    "time": "2017-10-14T20:34:30+00:00",
    "sport": "soccer",
    "owner": "john",
    "players": "12",
    "game_id": 5,
  }
];

of(...data).pipe(
  groupBy((p: any) => p.time.split('T')[0]),
  mergeMap(group$ =>
    group$.pipe(reduce((acc, cur) => [...acc, cur], [`${group$.key}`]))
  ),
  map(arr => ({ date: arr[0], games: arr.slice(1) })),
  toArray()
).subscribe(p => console.log(p));

结果:

enter image description here