簡體   English   中英

將數組切片為數組數組

[英]Slice Array into an array of arrays

如果我有一個功能:

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

  return slicedArray;
}

我正在尋找一個數組並將其切成數組數組..我將如何去做?

所以,如果我有這個:

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

結果應該是:

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

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

任何幫助表示贊賞。

使用常規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)); 

step選項指向每次提取的偏移量(切片)

這應該做到這一點。 它是一個簡單的遞歸函數,它從數組的開頭剪切n個元素,並使用其余元素調用自身。

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

試試這個,它將原始數組切成2個,然后連接到1個數組

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

嘗試這個:

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

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

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

  return result;
}

這會將數組分成幾塊,每塊都是size變量的size ,所以

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

將輸出

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

降低:

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

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM