Group an array to form three arrays based on the index

后端 未结 3 1521
梦谈多话
梦谈多话 2021-01-29 12:44

I want to sort an array in a way that it gives back three arrays. So

var myArray = [\'1\',\'2\',\'3\',\'1\',\'2\',\'3\',\'1\',\'2\',\'3\',\'1\',\'2\',\'3\',\'1\'         


        
3条回答
  •  抹茶落季
    2021-01-29 13:08

    var myArray = ['1','2','3','1','2','3','1','2','3','1','2','3','1','2','3','1','2','3','1','2','3','1','2','3','1','2','3','1','2','3','1','2','3','1','2','3','1','2','3','1','2','3','1','2','3','1','2','3','1','2','3','1','2','3'];
    
    var myArray1 = [];
    var myArray2 = [];
    var myArray3 = [];
    for(var i = 0; i < myArray.length;) {
        myArray1.push(myArray [i++]); 
        myArray2.push(myArray [i++]);
        myArray3.push(myArray [i++]);
    }
    

提交回复
热议问题