集合返回如下11項;創建正確的陣列結構
(1, "Ball", "Result1")
(2, "Ball", " Result2")
(3, "Ball", " Result3")
(4, "Ball", " Result4")
(5, "Ball", " Result5")
(6, "Ball", " Result6")
(7, "Ball", " Result7")
(8, "Ball", " Result8")
(9, "Pool", " Pool 1")
(10, "Pool", " Pool 2")
(11, "Pool", " Pool 3")
我想存儲它們,將它們作爲組四個項目。所以,我的陣列看起來像這樣
var data = [];
data.push({
myclass: "First4",
schedule: [ {
id : '1',
gameType: 'Ball',
result: 'Result11'
}, {
id: '2',
gameType: 'Ball',
result: 'Result2'
},........... ]
});
//second group
data.push({
divClass : "second4",
items : [ {
id : '5'
gameType: 'Ball',
result: 'Result5'
}, {
id : ''
gameType: 'Ball',
result: 'Result6
} ]
});
我怎麼可以寫一個for循環,這樣我可以動態地達到同樣的效果而不是寫推手動
for(var i = 0; i < collLength; i++){
// do push 1 with first four //data.push(class, sheculde first 4 items, result)
// do second push with second four
// do third push with the remaining
}
你是否真的想給你的類命名爲「first4」,「second4」等?通過數組索引動態分類「group0」,「group1」等等,其中數字來自數組索引似乎更簡單。 –
...但是對於你的循環,只要將'i ++'改成'i + = 4',然後在[i],'i + 1','i + 2','i + 3'循環。 –
我只是想清楚我的問題......我需要做的是,當我收到x個項目時,在這種情況下,我希望有x個小組(在這種情況下是3個),其中每個小組最多可容納4件物品。我只是不能寫出循環的權利動態地執行它...... – myTD