给定数据:
const pairs = [ {name: bob, value: foo}, {name: jane, value: foo}, {name: mary, value: foo}, {name: elizabeth, value: foo}, {name: colin, value: foo}]
希望获得以下输出:
const pairs = [
[{name: bob, value: foo}, {name: jane, value: foo}],
[{name: mary, value: foo}, {name: elizabeth, value: foo}, {name: colin, value: foo}]
]
const result = pairs.reduce((acc, curr, i, array) => {
const acc = acc.length % 3 === 0 ? acc.slice(acc.length -1, acc.length) : acc.push(curr);
return acc
}, []);
有没有一个简单的方法可以做到这一点数组.reduce()?