2677. Chunk Array
https://leetcode.com/problems/chunk-array/
Javascript
var chunk = function(arr, size) {
const result = []
arr.reduce((accu, value, index) => {
let current
if (index % size === 0) {
current = []
accu.push(current)
} else {
current = accu[accu.length-1]
}
current.push(value)
return accu
}, result)
return result
}
Typescript
function chunk(arr: any[], size: number): any[][] {
const result = []
arr.reduce((accu, value, index) => {
let current: any[]
if (index % size === 0) {
current = []
accu.push(current)
} else {
current = accu[accu.length-1]
}
current.push(value)
return accu
}, result)
return result
};