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))