0

This function will take an array and chunk it into separate arrays, create an offset at the beginning and wrap that in another array. The problem I have is not all of the numbers from the original array (arr1) are included in the chunks. You can see the output in the link I have provided below. It misses out numbers 5, 13, 21, 29 and 30. Can anyone explain why this happens?

function chunkifyArray(input, chunkSize, offset) {

    const output = [];

    let tmp = offset ? new Array(offset).fill('') : [];

    for(var i = 0; i < input.length; i++){
        if (tmp.length < chunkSize) {
            tmp.push(input[i]);
        } else {
            output.push(tmp);
            tmp = [];     
        }
    }

    return output;
}

var arr1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30'];
console.log(chunkifyArray(arr1, 7, 3));

https://jsbin.com/zucaguvoti/edit?js,console

1
  • Because you never push those values - you empty tmp insead... Commented Nov 20, 2018 at 19:57

2 Answers 2

2

Because you never push those values - you empty tmp insead… Try this:

for(var i = 0; i < input.length; i++){
    tmp.push(input[i]);
    if (tmp.length == chunkSize) {
        output.push(tmp);
        tmp = [];     
    }
}

Edited to ensure the last chunk is pushed onto output...

Sign up to request clarification or add additional context in comments.

1 Comment

This would still miss the last chunk. The push should come first.
0

I changed little bit your code to get to the desired result. You can use the % operator as well as remember to add the last portion at the end of the for loop:

var data = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30'];

function chunkifyArray(input, chunkSize, offset=0) {
  let output = [], tmp = [];
  input = new Array(offset).fill('').concat(input);
  for (var i = 0; i < input.length; i++) {
    tmp.push(input[i])
    if (i && i%chunkSize == 0) {
        output.push(tmp);
	tmp = []
    }
  }
  output.push(tmp);  // add the remaining ones
  return output;
}

console.log(chunkifyArray(data, 7, 3));

You could chunk an array with ES6 and Array.reduce:

const data = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30']

const chunkBy = (arr, by=2, offset=0) => new Array(offset).fill('').concat(arr)
 .reduce((r,c,i) => (i%by==0 ? r.push([c]) : r[r.length-1] = [...r[r.length-1], c], r), [])
		
console.log(chunkBy(data, 7, 3))

When not in compact form this would look like:

const data = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30']

const chunkBy = (arr, by=2, offset=0) => {
   arr = new Array(offset).fill('').concat(arr)
   return arr.reduce((r,c,i) => {
     if(i%by==0)
       r.push([c])
     else 
      r[r.length-1] = [...r[r.length-1], c]
     return r
   }, [])
}
		
console.log(chunkBy(data, 7, 3))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.