How do I group items in an array by date?

后端 未结 3 1797
谎友^
谎友^ 2021-01-01 19:13

Given the following array of objects:

[
{
\"notes\": \"Game was played\",
\"time\": \"2017-10-04T20:24:30+00:00\",
\"sport\": \"hockey\",
\"owner\": \"steve\         


        
相关标签:
3条回答
  • 2021-01-01 19:18

        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)

    0 讨论(0)
  • 2021-01-01 19:25

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

    0 讨论(0)
  • 2021-01-01 19:35

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

    0 讨论(0)
提交回复
热议问题