Javascript:.push 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33177899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: .push is not a function
提问by Devilius
I am having a problem with my code:
我的代码有问题:
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)
As far as I understand, this is because it is treating the array argument as something other than an array. However, I thought I fed it the variable "current" which is an array. Can someone explain the problem? Thanks.
据我了解,这是因为它将数组参数视为数组以外的东西。但是,我想我给它输入了变量“current”,它是一个数组。有人可以解释这个问题吗?谢谢。
回答by Rocket Hazmat
Array.push
doesn't return an array. It returns the newlength of the array it was called on.
Array.push
不返回数组。它返回调用它的数组的新长度。
So, your return array.push(b);
returns an int
. That int gets passed back as array
... which is not an array so it doesn't have a .push()
method.
因此,您return array.push(b);
返回一个int
. 该 int 被传回为array
... 这不是数组,因此它没有.push()
方法。
You need to do:
你需要做:
array.push(b);
return array;
回答by Leon Gaban
Return just the Array, see below:
只返回数组,见下文:
http://jsfiddle.net/0en82r7t/1/
http://jsfiddle.net/0en82r7t/1/
var arrays = [[1, 2, 3], [4, 5], [6]];
console.log(reduce(arrays,function(array,b){
array.push(b);
return array;
}));
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) {
array.push(b)
return array;
}));
array.push does not return an Array, but instead the new length
array.push 不返回数组,而是返回新长度
Also, I know this is just a test, but in the future and in real app development don't name an Array
array. Use more verbose and clear naming, examples: numGroupArray, datesArray, timeArray, tagsArray...
另外,我知道这只是一个测试,但在未来和实际应用程序开发中不要命名Array
数组。使用更详细和清晰的命名,例如:numGroupArray、datesArray、timeArray、tagsArray...
回答by sfandler
The problem here is that array.push(b)
returns the new length of the array
. So after calling combine(current, array[i])
for the first time, the length of your array will be returned and current
becomes an integer
, and since current
is the passed to combine(current, array[i])
in the next iteration, JavaScript throws the TypeError
. Your implementation for combine(current, array[i]
should look like this:
这里的问题是array.push(b)
返回array
. 所以在combine(current, array[i])
第一次调用之后,你的数组的长度将被返回并current
变成一个integer
,并且由于current
是combine(current, array[i])
在下一次迭代中传递给,JavaScript 抛出TypeError
. 您的实现combine(current, array[i]
应如下所示:
function(array, b) {
array.push(b);
return array;
}
回答by Youssef
ES6 solution :
ES6 解决方案:
reduce([1, 2, 3, 4], (array, b) => [...array, b], [])
回答by Sushant Kafle
Looks like there is a problem in your implementation:
看起来你的实现有问题:
function(array, b) {
return array.push(b);
}
What you are returning from this function is not a array.
您从此函数返回的不是数组。
return array.push(b);
returns the length of the array after performing the push.
执行推送后返回数组的长度。
So, you should modify your function to:
因此,您应该将函数修改为:
function(array, b) {
array.push(b);
return array;
}
This might do the trick. Goodluck!
这可能会奏效。祝你好运!
回答by Justin Schultz
I don't understand what the function reduce is doing, but the problem is that Array.prototype.push returns the length of the array.
我不明白 reduce 函数在做什么,但问题是 Array.prototype.push 返回数组的长度。
var array = [1,2,'b'];
var val = array.push('foo');
// val === 4;
Therefore, instead of re-setting current every iteration of your loop, just simply call the function since .push()
modifies the array in place.
因此,无需在循环的每次迭代中重新设置当前值,只需简单地调用该函数即可,因为它.push()
会就地修改数组。
function reduce(array,combine){
var current = [];
for(var i = 0;i<array.length;i += 1){
combine(current,array[i]);
}
return current;
}