将数组切片为数组数组

时间:2017-01-31 18:12:44

标签: javascript arrays

如果我有一个功能:

function sliceArrayIntoGroups(arr, size) {
  var slicedArray = arr.slice(0, size);

  return slicedArray;
}

我正在寻找一个数组并将其切成数组数组..我该怎么做?

所以,如果我有这个:

sliceArrayIntoGroups(["a", "b", "c", "d"], 2);

结果应为:

[["a","b"],["c","d"]]

但我不知道如何在切片后保存原始数组的第二部分。

感谢任何帮助。

6 个答案:

答案 0 :(得分:5)

使用常规while循环和自定义step参数的解决方案:



function sliceArrayIntoGroups(arr, size) {
  var step = 0, sliceArr = [], len = arr.length;
  while (step < len) {
    sliceArr.push(arr.slice(step, step += size));
  }
  return sliceArr;
}

console.log(sliceArrayIntoGroups(["a", "b", "c", "d"], 2));
console.log(sliceArrayIntoGroups(["a", "b", "c", "d", "e", "f"], 2));
console.log(sliceArrayIntoGroups(["a", "b", "c", "d", "e", "f"], 3));
&#13;
&#13;
&#13;

step选项指向每次提取的偏移量(切片)

答案 1 :(得分:2)

这应该这样做。它是一个简单的递归函数,它从数组的开头对 n 元素进行切片,并使用其余元素调用自身。

&#13;
&#13;
function sliceArrayIntoGroups(arr, size) {
  if (arr.length === 0) { return arr; }
  return [ arr.slice(0, size), ...sliceArrayIntoGroups(arr.slice(size), size) ];
}

console.log(sliceArrayIntoGroups([1,2,3,4,5], 2));
console.log(sliceArrayIntoGroups([1,2,3,4,5], 3));
console.log(sliceArrayIntoGroups([1,2,3,4,5], 10));
&#13;
&#13;
&#13;

答案 2 :(得分:2)

试试这个,它将原始数组切片为2个,然后连接到1个数组

&#13;
&#13;
function sliceArrayIntoGroups(arr, size) {
  if (size >= arr.length || size <= 0) { return arr; }
  return [arr.slice(0, size), arr.slice(size)];
}
console.log(sliceArrayIntoGroups(["a", "b", "c", "d"], 2));
&#13;
&#13;
&#13;

答案 3 :(得分:1)

试试这个:

&#13;
&#13;
 function sliceArrayIntoGroups(arr, size) {
   var result = [];

   while (arr.length > 0) {
     result.push(arr.splice(0, size));
   }

   return result;
 }


 console.log(sliceArrayIntoGroups(["a", "b", "c", "d", "e", "f"], 3));
 console.log(sliceArrayIntoGroups(["a", "b", "c", "d"], 2));
&#13;
&#13;
&#13;

function sliceArrayIntoGroups(arr, size) {
  var result = [];

  while (arr.length > 0) {
    result.push(arr.splice(0, size));
  }

  return result;
}

这会将数组划分为多个部分,其中每个部分的大小为size变量,所以

sliceArrayIntoGroups(["a", "b", "c", "d", "e", "f"], 3);

将输出

[["a", "b", "c"], ["d", "e", "f"]]

答案 4 :(得分:0)

减少

var x = [1,2,3,4,5,6,7,8,9];
var chunk = function(arr,n) {
    var temp;
    return arr.reduce(function(carry,item,index) {

        //if we're at a chunk point: index%n == 0
        if(!(index%n)) { 

            //if temp currently holds items, push it onto carry
            if(temp && temp.length) { carry.push(temp); }

            //reset temp to an empty array
            temp = []; 
        }

        //push the current item onto temp
        temp.push(item);

        //if this is the last item in the array, push temp onto carry
        index == arr.length-1 && carry.push(temp);

        return carry;
    },[]);
};

chunk(x,5);

答案 5 :(得分:0)

Javascript slice()方法返回数组中的选定元素,作为新的数组对象。因此,使用for循环创建smallArray并将它们推送到arrGroup数组。

function sliceArrayIntoGroups(arr, size) {
  let arrGroup =[];
  for (let i=0; i<arr.length; i+=size) {
    let smallArray = arr.slice(i,i+size);//creating smaller array of required size using slice
    arrGroup.push(smallArray);
  }
  return arrGroup;
}