集合返回如下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