const items = ['a', 'b', 'c', 'd']
const reduced = items.reduce((acc, cur, index) => {
const arrayIndex = Math.ceil((index + 1) / 3) - 1
if (acc[arrayIndex]) {
acc[arrayIndex].push(cur)
} else {
acc.push([cur])
}
return acc
}, [])
I'm taking an array of items, batching them into arrays of three at most and returning them as arrays of array. Here reduced
yields the correct structure of [["a", "b", "c"], ["d"]]
. How do I accomplish this without the if statement (which mutates the accumulated value directly)?
a = [...a]
egvar b='abcd'.split("").reduce((a,v,i)=>((a=[...a][i/3|0]=i%3?a[i/3|0]:[])[i%3]=v,a),[]);
Sorry a little squished to fit the comment. \$\endgroup\$