Devilius
Devilius

Reputation: 321

Javascript: .push is not a function

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.

Upvotes: 10

Views: 23103

Answers (7)

Arkadiusz Kulpa
Arkadiusz Kulpa

Reputation: 119

I know this is old and practically already solved, but It didn't solve my issue directly. When searching for solutions I discovered another way to go avoid .push() method if we wanted to directly return the array.

return [ ...array, b];

as explained in this video at 1:41:00 https://www.youtube.com/watch?v=oBt53YbR9Kk&t=199s

Upvotes: 1

Hayi
Hayi

Reputation: 6236

ES6 solution :

reduce([1, 2, 3, 4], (array, b) => [...array, b], [])

Upvotes: 1

Leon Gaban
Leon Gaban

Reputation: 39018

Return just the Array, see below:

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

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...

Upvotes: 3

Sushant Kafle
Sushant Kafle

Reputation: 446

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!

Upvotes: 0

sfandler
sfandler

Reputation: 640

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:

function(array, b) {
    array.push(b);
    return array;
}

Upvotes: 3

Justin Schultz
Justin Schultz

Reputation: 440

I don't understand what the function reduce is doing, but the problem is that Array.prototype.push returns the length of the array.

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.

function reduce(array,combine){
  var current = [];
  for(var i = 0;i<array.length;i += 1){
    combine(current,array[i]);
  }
  return current;
}

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227220

Array.push doesn't return an array. It returns the new length of the array it was called on.

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.

You need to do:

array.push(b);
return array;

Upvotes: 18

Related Questions