implement _.chunk()
2022年5月1日小于 1 分钟
implement _.chunk()
Question
_.chunk() splits array into groups with the specific size.
Please implement your chunk(arr: any[], size: number)
chunk([1,2,3,4,5], 1)
// [[1], [2], [3], [4], [5]]
chunk([1,2,3,4,5], 2)
// [[1, 2], [3, 4], [5]]
chunk([1,2,3,4,5], 3)
// [[1, 2, 3], [4, 5]]
chunk([1,2,3,4,5], 4)
// [[1, 2, 3, 4], [5]]
chunk([1,2,3,4,5], 5)
// [[1, 2, 3, 4, 5]]
for size smaller than 1, return an empty array.
Code
/**
* @param {any[]} items
* @param {number} size
* @returns {any[][]}
*/
function chunk(items, size) {
// your code here
}