2017-10-18 255 views
0

鑑於以下對象的數組:如何按日期對數組中的項目進行分組?

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

我的版本返回與日期爲值的數組,然後裏面的那些的遊戲列出,但我想列出它們在不同的鍵相同的對象。

任何幫助或指導將不勝感激。

+1

你嘗試過這麼遠嗎?這裏的解決方案非常簡單。 –

+0

嗨斯賓塞,我不確定如何在評論中發佈代碼,但我創建了一個空數組,並運行了for循環來遍歷第一個信息。我剪切原始日期以獲取(yyyy-mm-dd)格式,然後創建另一個for循環來檢查當前空數組中是否存在該日期,如果沒有,我將爲此創建一個新的密鑰日期,然後將任何遊戲傳入遊戲密鑰。起初,我得到一個無限循環,我的記憶崩潰了,然後我一直覆蓋我以前的對象。 – Blake

+0

您不想在評論中發佈代碼,而是將其作爲編輯添加到問題中。 –

回答

2

以下是使用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);

+0

嗨,奧斯汀,謝謝你的回答。我可以使用for循環獲得類似的答案,但我需要的是將日期作爲自己的鍵,並將遊戲作爲另一個鍵的值列入數組中(全部在同一對象內) 。所以{date:'date',games:[...]} – Blake

+0

@Blake明白了,只是看到了不同的格式。我認爲我剛剛添加的更新應該會給你你想要的。 –

+0

非常感謝您的幫助奧斯汀,這個問題一直讓我頭痛! – Blake

0

這是你在找什麼???

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

嗨鮑比,謝謝你花時間回答我。我已經以類似的格式獲得它。我需要的格式(奧斯汀在上面正確回答)是:[{date:'date string',games:[這個日期的遊戲陣列]}]。 – Blake

+0

哦,我明白了; D快樂編碼! – bobbyanne

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)

相關問題