web-dev-qa-db-ja.com

配列内のアイテムを日付でグループ化するにはどうすればよいですか?

次のオブジェクトの配列があるとします。

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

私のバージョンは日付を値として持つ配列を返し、それらの中にゲームがリストされていますが、同じオブジェクトに異なるキーとしてリストしようとしています。

どんな助けや指導も大歓迎です。

7
Blake

これはreduceを使用した解決策です。 timeを日付文字列に分割し、各日付のキーを設定します。キーが存在する場合は、それを配列にプッシュします。

更新配列形式のバージョンを追加

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);
27
Austin Greco

これはあなたが探しているものですか???

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();
3
bobbyanne
    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)
2
Prasanna