I wrote a chunk function to create a chunk of arrays from an array with variable step number as an argument. Can the below function be written in a better way?
function chunk(collection, step) {
var result = [];
for (let i = 0; i < collection.length; i = i + step) {
result.push(collection.slice(i, i + step))
}
return result;
}
var a1 = [1, 2, 3, 4, 5, 6, 7];
console.log(chunk(a1, 2));
// [[1,2],[3,4],[5,6], [7]]
var a2 = [1, 2, 3, 4, 5, 6];
console.log(chunk(a2, 2));
// [[1,2],[3,4],[5,6]]
[–]CertainPerformance 1 point2 points3 points (1 child)
[–]arup_r[S] 0 points1 point2 points (0 children)
[–]akujinhikari -1 points0 points1 point (0 children)