3

Assume that you have an array and want to divide it by chunks of 3. If the array is..

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];

...the new array would be

let newArr = [1, 4, 7, 10, 13,   2, 5, 8, 11,   3, 6, 9, 12]

// In other words:
1  2  3
4  5  6
7  8  9
10 11 12
13

The chunking part should be this code (if sorted like newArr):

let chunkedArr = _.chunk(_.toArray(newArr), 3);

...however I couldn't figure out how to sort the arr to newArr to be able to chunk in the right order. What is the proper way of handling such case?

Please note that the integers are just pseudo and I will use proper objects of array.

senty
  • 12,385
  • 28
  • 130
  • 260

4 Answers4

5

One option is to use ES6 reduce to group the array into a multidimensional array. Use concat to flatten the multidimensional array.

[].concat(...) - basically flattens the multidimensional array. Starting with an empty array [], you concat each secondary array. Use the spread operator (...) to reiterate each secondary array and concat each.

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
let groups = 3;

let newArr = [].concat(...arr.reduce((c, v, i) => {
  let k = i % groups;
  c[k] = c[k] || [];
  c[k].push(v);
  return c;
}, []));

console.log(newArr);
Eddie
  • 26,593
  • 6
  • 36
  • 58
3

Please try the following (jsfiddle):

//This version works for variable chunk sizes as well.
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
alert(chunks(arr, 3)); //You can update chunk size here

function chunks(arr, chunkSize) {
  var result = [];
  var index = 0;
  for (var i = 0; i < chunkSize; i++) {
    for (var j = 0; j < arr.length / chunkSize; j++) {
      if (arr[i + (chunkSize * j)] != null)
        result[index++] = arr[i + (chunkSize * j)];
    }
  }
  return result;
}

//This version only works for chunks of size 3.
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
let subArr1 = [];
let subArr2 = [];
let subArr3 = [];
var result = [];
var i = 0, j = 0, k = 0;

for (var index = 0; index < arr.length; index++) {
  if (index % 3 == 0) {
    subArr1[i++] = arr[index];
  }
  if (index % 3 == 1) {
    subArr2[j++] = arr[index];
  }
  if (index % 3 == 2) {
    subArr3[k++] = arr[index];
  }
}

result.push(subArr1, subArr2, subArr3);
alert(result);
Mikaal Anwar
  • 1,720
  • 10
  • 21
  • This returns `[1, 4, 7, 10, 13, 2, 5, 8, 11, 14, 2, 5, 8, 11, 14]` though, so it has duplicate values :/ _Please keep in mind that 13 is not explicitly the final index and it should work consistently for infinite elements_ – senty May 26 '18 at 03:55
  • Please recheck. It was a minor typo in the second last line. I was appending the second sub-array twice. And yes, this would work for any given length of the array. – Mikaal Anwar May 26 '18 at 03:59
  • 1
    This is also very nice! – senty May 26 '18 at 04:05
  • 1
    **Additionally:** `result.push(subArr1, subArr2, subArr3);` would actually work pretty efficiently for dividing stuff into chunk of 3 – senty May 26 '18 at 04:08
  • 1
    Thanks! I have made yet another version with plain javascript that can work for variable chunk sizes as well. Please check out: https://jsfiddle.net/ke1c8sa5/2/ – Mikaal Anwar May 26 '18 at 04:50
0

Please check this it may help you. I also take a reference from here. Please check and let us know. any thing else you need.

Thanks

Here is the sample code.

var i,j,resArr,chunkSize = 10; for (i=0,j=array.length; i<j; i+=chunk) { resArr = array.slice(i,i+chunk); }

biswajit-rout
  • 377
  • 2
  • 11
0
const original = [1,2,3,4,5,6,7,8,9,10,11,12,13]
const chunks = 3

function getChunckedArr(arr, n) {
  let sub = new Array(n);
  let result = [];

  for (let i=0; i<sub.length; i++)
    sub[i] = []

  arr.forEach(function (val, index){
    let o = (index % n);
    sub[o][sub[o].length] = val;
  });

  for (let i=0; i<sub.length; i++)
    result.push.apply(result, sub[i]);

  return result;
}

const chunked = getChunckedArr(original, chunks);
Dan Nagle
  • 4,384
  • 1
  • 16
  • 28