这是一个免费代码营问题(我自己解决了 - 不太好看,我知道),但是一些数组方法并没有像我想象的那样工作(通过阅读MDN上的文档) 。我的问题是关于slice()和Array.of():问题出现在相关代码旁边的注释中。谢谢!
说明:编写一个将数组(第一个参数)拆分为大小长度(第二个参数)的函数,并将它们作为二维数组返回。
function chunkArrayInGroups(arr, size) {
var newArray = [];
for (i = 0; i < (arr.length) / size; i++) {
var slicedArray = arr.slice(size * i, size + (size * i));
console.log(typeof slicedArray); //This returns "object."
Array.of(slicedArray); //But this returns "undefined"--even though my understanding (albeit shallow) is that *Array.of()* casts the object as an array.
newArray.push(slicedArray);
}
return newArray; // I tried to return *"This is the new array: "+ newArray*, but it seemed to case *newArray* as a string. Why? How to fix?
}
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2);
&#13;