JavaScript一个一维数组变为两个一维数组

  
//例如[1,2,3,4,5,6,7,8]变为[[1,2,3,4],[5,6,7,8]]
var arr=[1,2,3,4,5,6,7,8]; function fixedArray(arr) { var bigArray = []; var smArray = []; for (var i = 0; i < arr.length; i++) { smArray.push(arr[i]);
          //四个一组放到新数组
if ((i + 1) % 4 == 0 || i == arr.length - 1) { bigArray.push(smArray); smArray = []; } } return bigArray; } console.log(fixedArray(arr))

 

posted @ 2019-05-09 17:22  Samsara315  阅读(520)  评论(0编辑  收藏  举报