我遇到了代码问题:
var arrays = [[1, 2, 3], [4, 5], [6]];
console.log(reduce(arrays,function(array,b){
return array.push(b);
}));
function reduce(array,combine){
var current = [];
for(var i = 0;i<array.length;i += 1){
current = combine(current,array[i]);
}
return current;
}
console.log(reduce([1, 2, 3, 4], function(array, b) {
return array.push(b);
}));
// → [1, 2, 3, 4, 5, 6]
I get this error:
TypeError: array.push is not a function (line 3)
据我所知,这是因为它将数组参数视为与数组不同的东西。然而,我认为我已经传递了变量“current”,它是一个数组。有人能解释一下问题吗?谢谢。
array.push
返回下一个可用的索引,不能在数字上使用push
。 - Tusharvar flattened = [].concat.apply([],arrays)
来获得相同的结果吗? - epascarelloconsole.log(array)
,因为这个变量和对它的.push()是你得到这个错误的地方。 - Julian Fondren