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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46802448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:56:07  来源:igfitidea点击:

How do I group items in an array by date?

javascriptarraysfor-loopiterationgrouping

提问by Blake

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.

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

回答by Austin Greco

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

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

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

回答by bobbyanne

Is this what you're looking for???

这是你要找的???

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

回答by Prasanna

    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)