Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 创建正确的数组结构_Javascript_Loops - Fatal编程技术网

Javascript 创建正确的数组结构

Javascript 创建正确的数组结构,javascript,loops,Javascript,Loops,一个集合返回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")

一个集合返回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

  }
for(变量i=0;i
var indata=[…];//这是您的对象列表。
var-outdata=[];
var n=indata.length;
对于(变量i=0;i
var数据=[];
对于(变量i=0;i
这里有一种使用
.reduce()
的方法


你真的想把你的班级命名为“first4”、“second4”等吗?根据数组索引动态地将它们分类为“group0”、“group1”等似乎更简单,因为数字来自数组索引……但是对于循环,只需将
i++
更改为
i+=4
,然后访问
i
i+1
i+2
,即可,
i+3
在循环中。我只是想把我的问题弄清楚。。。我需要做的是,当我收到x个数量的项目时,在本例中为11,我希望有x个数量的组(在本例中为3),其中每个组最多容纳4个项目。我只是无法编写正确的for循环来动态执行它…是的,只需使循环增量4,并访问当前索引和接下来三个索引处的项。只需调整
.push()
即可,但这一部分似乎有手柄。感谢您的回复。它们都在工作,但是否有一个不使用“切片”的方法来处理这个问题?
var indata = [...]; // this is filled with your list of objects.
var outdata = [ ];

var n = indata.length;

for (var i = 0; i < n; i += 4)
{
    outdata.push({
        class: 'group' + outdata.length, 
        items: indata.slice(i, i + 4)
    });
}
var data = [];

for(var i = 0; i < collLength; i+=4) {
    data.push({
        divClass: "group" + (i / 4),
        items: collection.slice(i, i + 4).map(function(item) {
            return {id:item[0], gameType:item[1], result:item[2]};
        })
    });
}
var indata = [...]; // this is filled with your list of objects.
var outdata = [ ];

function mapper(element, i)
{
     var j = Math.floor(i / 4);

     if (outdata[j] == undefined)
         outdata[j] = { class: 'group' + j, items: [ ] };

     outdata[j].items.push(element);
}

indata.map(mapper);
var data = collection.reduce(function(arr, obj, i) {
    return i % 4 ? arr : 
                   arr.concat({
                      divClass: "group" + (i / 4),
                      items: collection.slice(i, i + 4).map(function(item) {
                          return {id:item[0], gameType:item[1], result:item[2]};
                      })
                   });
}, []);