最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Write a function that splits an array (first argument) into groups the length of size (second argument) and returns

programmeradmin1浏览0评论

I am working through a javascript problem that asks me to:

Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a multidimensional array.

For example, the input

chunk([0, 1, 2, 3, 4, 5], 2)

should return the 'chunked arrays': [[0, 1], [2, 3], [4, 5]]

I can get it to work for most examples but when there are more than 2 chunks it switches the order and I am not sure why. Here is the code that I have written:

function chunk(arr, size) {
  var newArray = [],
      i, temp = arr;
  for (i = 0;i<= arr.length-size;i+=size){
    newArray.push(arr.slice(i,i+size));
    temp.splice(i,size);
  }
  newArray.push(temp);
  return newArray;
}
chunk(['a', 'b', 'c', 'd'], 2);

I am working through a javascript problem that asks me to:

Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a multidimensional array.

For example, the input

chunk([0, 1, 2, 3, 4, 5], 2)

should return the 'chunked arrays': [[0, 1], [2, 3], [4, 5]]

I can get it to work for most examples but when there are more than 2 chunks it switches the order and I am not sure why. Here is the code that I have written:

function chunk(arr, size) {
  var newArray = [],
      i, temp = arr;
  for (i = 0;i<= arr.length-size;i+=size){
    newArray.push(arr.slice(i,i+size));
    temp.splice(i,size);
  }
  newArray.push(temp);
  return newArray;
}
chunk(['a', 'b', 'c', 'd'], 2);
Share Improve this question asked May 31, 2015 at 18:40 RoseRose 2405 silver badges14 bronze badges
Add a ment  | 

10 Answers 10

Reset to default 7

Another version:

function chunk(arr, size) {
    var result = [];
    while (arr.length > size) {
        result.push(arr.splice(0, size))
    }
    if (arr.length)
        result.push(arr);

    return result;
}
function chunk(arr, size) {
  var newArr = []; // New empty array
  while (arr.length > 0) { // Loop thru the array items
    newArr.push(arr.splice(0, size)); // Removes first 2 items then add it to new array
  }
  return newArr; // New 2D array
}

I tried using these lines of codes in console and it worked perfectly fine.

The problem is that you are both advancing i by size at each iteration and removing size elements from position i. This causes you to skip processing every other chunk. Also, your continuation condition should be i < arr.length-size; as it is, you would need to test for an empty temp before pushing it after the loop exits. Try rewriting your loop as:

for (i = 0; i < arr.length-size;){
    newArray.push(arr.slice(i,i+size));
    temp.splice(i,size);
}

Since i is always at 0, the loop can be simplified to:

while (arr.length > size) {
    newArray.push(arr.slice(0,size));
    temp.splice(0,size);
}
function chunk(arr, size) {

  var arr1=[];
  var j=0,temp=size;
  var len = arr.length/size;
  for (var i=0; i<len; i++){
    arr1.push(arr.slice(j,size));
    j+=temp;
    size+=temp;
  }
  return arr1;
}

chunk([0, 1, 2, 3, 4, 5], 2);

This is my solution:

function chunkArrayInGroups(arr, size) {
  var acum=[];
  for(i=0;i<arr.length;i=i+size)
  {
    var vec=arr.slice(i,size+i);
    acum.push(vec);
  }

  return acum;
}

So many Ways...

function chunkArrayInGroups(arr, size) {
 var value = [];

for(var i =0; i<arr.length; i += size){
 value.push(arr.slice(i, size+i));
}

return value;
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);
function chunk(arr, size) {
  var newArr = [];
  // x is less than or equals arr.length.
  for(var x = 0; x <= arr.length; x++){
     newArr.push(arr.splice(0, size));
  }

  if (arr.length){
     newArr.push(arr);
  }
  return newArr;
}

chunk(['a', 'b', 'c', 'd'], 2);
function chunkArrayInGroups(arr, size) {
  var result = [],
      iterations = Math.ceil(arr.length/size);
  for (var i =0; i<iterations;i++){
      var sub = arr.splice(0, size);
      result.push(sub);
  }
  return result;       
}
    function chunkArrayInGroups(arr, size) {

  var temp = [];
  var result = [];

  for (var a = 0; a < arr.length; a++) {
    if (a % size !== size - 1)
      temp.push(arr[a]);
    else {
      temp.push(arr[a]);
      result.push(temp);
      temp = [];
    }
  }

  if (temp.length !== 0)
    result.push(temp);
  return result;
}
function chunkArrayInGroups(arr, size) {
  var newarr=[];
  var num=0;
  var szie=0;
  var size2=size;
  if(arr.length%size==0){
    szie=arr.length/size;
  }
  else{
    var floor=arr.length/size;
    szie=Math.floor(floor)+1;
  }
  for (let i=0;i<szie;i++){
      newarr.push(arr.slice(num,size));
      num=num+size2;
      size=size+size2;

  }
  return newarr;
}
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论