Open zilongxuan001 opened 6 years ago
数组元素按指定长度划分为多个二维数组。比如说,指定长度为3,则数组元素三个为一组,成为一个子数组。
function chunkArrayInGroups(arr, size) { // Break it up. var arrDim=[]; for(var i=0;i<arr.length;i+=size){ var length=i+size; var arrSlice=arr.slice(i,length); arrDim.push(arrSlice); } return arrDim; } chunkArrayInGroups(["a", "b", "c", "d"], 2);
Array.prototype.slice() Array.prototype.push()
https://www.freecodecamp.org/challenges/chunky-monkey
挑战
数组元素按指定长度划分为多个二维数组。比如说,指定长度为3,则数组元素三个为一组,成为一个子数组。
代码
结果显示
帮助
Array.prototype.slice() Array.prototype.push()
来源
https://www.freecodecamp.org/challenges/chunky-monkey