如何按日期对数组中的项目进行分组? [英] How do I group items in an array by date?

查看:31
本文介绍了如何按日期对数组中的项目进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下对象数组:

<预><代码>[{"notes": "游戏已玩","时间": "2017-10-04T20:24:30+00:00","sport": "曲棍球","所有者": "史蒂夫","玩家": "10","game_id": 1,},{"notes": "游戏已玩","时间": "2017-10-04T12:35:30+00:00","sport": "长曲棍球","所有者": "史蒂夫","玩家": "6","game_id": 2,},{"notes": "游戏已玩","时间": "2017-10-14T20:32:30+00:00","sport": "曲棍球","所有者": "史蒂夫","玩家": "4","game_id": 3,},{"notes": "游戏已玩","时间": "2017-10-04T10:12:30+00:00","sport": "曲棍球","所有者": "亨利","玩家": "10","game_id": 4,},{"notes": "游戏已玩","时间": "2017-10-14T20:34:30+00:00","sport": "足球",所有者":约翰","玩家": "12","game_id": 5,}]

我正在尝试创建一个新数组,该数组将创建一个对象,其中每个日期(作为键)以及在同一对象中的游戏(另一个键)下列出的该日期玩的任何游戏.

新数组应如下所示:

<预><代码>[{日期:'日期字符串',游戏:[当天玩的游戏数组]},{日期:'其他日期',游戏:[其他日期下的游戏]}]...

这是我所做的:

let t = this.state.data;(以上数据)让列表 = [];for (让 i = 0; i < t.length; i++) {让日期 = t[i].time.slice(0,10);如果(!列表[日期]){列表[日期] = [];}列表[日期].push(t[i]);}

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

任何帮助或指导将不胜感激.

解决方案

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

更新添加数组格式版本

const data = [{notes: 'Game was Played', time: '2017-10-04T20:24:30+00:00', Sport: 'hockey', owner: 'steve', player: '10', game_id: 1},{ 注释:'游戏已玩',时间:'2017-10-04T12:35:30+00:00',运动:'长曲棍球',所有者:'史蒂夫',玩家:'6',游戏 ID:2 },{ 注释:'游戏已玩',时间:'2017-10-14T20:32:30+00:00',运动:'曲棍球',所有者:'史蒂夫',玩家:'4',游戏 ID:3 },{ 注释:'游戏已玩',时间:'2017-10-04T10:12:30+00:00',运动:'曲棍球',所有者:'亨利',玩家:'10',游戏 ID:4 },{ 注释:'游戏已玩',时间:'2017-10-14T20:34:30+00:00',运动:'足球',所有者:'约翰',球员:'12',游戏 ID:5 }];//这给出了一个以日期为键的对象const groups = data.reduce((groups, game) => {const date = game.time.split('T')[0];如果(!组[日期]){组[日期] = [];}组[日期].推(游戏);返回组;}, {});//改为以数组格式添加它const groupArrays = Object.keys(groups).map((date) => {返回 {日期,游戏:团体[日期]};});console.log(groupArrays);

Given the following array of objects:

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

I am trying to create a new array, that would create an object with each date(as a key) and any games played on that date listed under games(another key) in the same object.

The new array should look like:

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

Here is what I have done:

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

My version returns a array with the dates as the values, and then inside of those the games are listed, but i am trying to list them in the same object as different keys.

Any help or guidance would be greatly appreciated.

解决方案

Here's a solution using reduce. Split the time into a date string, and then set a key for each date. If the key exists push it onto the array:

Update added the array format version

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

这篇关于如何按日期对数组中的项目进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆