我有一个数组 [1,2,3,4,5,6,7 8,9,10,11,12,13]
我想把它分成小块,但大小不一样,我想每三个项目分块,然后一个,然后三个,依此类推。
以下代码将数组分块为三个大小
const chunk = (cards) => {
var chunkArray = [];
for(var i = 0; i < cards.length; i += 3) {
chunkArray.push(cards.slice(i, i+3));
}
return chunkArray;
}
cards = [1,2,3,4,5,6,7,8,9,10,11,12,13];
console.log(JSON.stringify(chunk(cards)));
结果
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11], [12, 13]]
我想要的结果
[[1, 2, 3, 4], [5], [6, 7, 8, 9], [10], [11, 12, 13]]