I have an array
const myArray = [1, 2, 2, 2, 3, 3, 3, 4, 4, 4];
that I want to split into smaller arrays. I am using lodash chunk to do it.
_.chunk(myArray, 3);
this will return
[1, 2, 2], [2, 3, 3], [3, 4, 4], [4]
but I would like it to return
[1], [2, 2, 2], [3, 3, 3], [4, 4, 4]
my solution was this
_.chain(myArray).reverse().chunk(3).reverse().value()
it reverses the array, splits it and then reverses it again. But is there a better way to do this? So chunk starts from the end and not the start.
Runnable example:
const myArray = [1, 2, 2, 2, 3, 3, 3, 4, 4, 4];
console.log(_.chunk(myArray, 3));
// => [1, 2, 2], [2, 3, 3], [3, 4, 4], [4]
console.log(_.chain(myArray).reverse().chunk(3).reverse().value());
// => [1], [2, 2, 2], [3, 3, 3], [4, 4, 4]
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Find the remainder, and if there's remainder, slice it from the left side, and combine it with the chunks of the rest of the array. If no remainder, chunk normally:
const myArray = [1, 21, 22, 23, 31, 32, 33, 41, 42, 43];
const chunkRight = (arr, size) => {
const rm = arr.length % size;
return rm ?
[arr.slice(0, rm), ..._.chunk(arr.slice(rm), size)]
:
_.chunk(arr, size);
};
const result = chunkRight(myArray, 3);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>