我正在编写一个函数,将数组(第一个参数)拆分为大小的长度(第二个参数),并将它们作为多维数组返回.
以便
1. chunk(["a", "b", "c", "d"], 2) should return [["a", "b"], ["c", "d"]].
2. chunk([0, 1, 2, 3, 4, 5], 3) should return [[0, 1, 2], [3, 4, 5]].
3. chunk([0, 1, 2, 3, 4, 5], 2) should return [[0, 1], [2, 3], [4, 5]].
4. chunk([0, 1, 2, 3, 4, 5], 4) should return [[0, 1, 2, 3], [4, 5]].
Run Code Online (Sandbox Code Playgroud)
我的功能出了什么问题?
任何建议和帮助表示赞赏.
function chunk(arr, size) {
var newArray2 = [];
var len = 0;
for(i=0; i < Math.ceil(arr.length/size); …Run Code Online (Sandbox Code Playgroud)