Cannot read property 'push' of undefined when combining arrays

492,332

Solution 1

You get the error because order[1] is undefined.

That error message means that somewhere in your code, an attempt is being made to access a property with some name (here it's "push"), but instead of an object, the base for the reference is actually undefined. Thus, to find the problem, you'd look for code that refers to that property name ("push"), and see what's to the left of it. In this case, the code is

if(parseInt(a[i].daysleft) > 0){ order[1].push(a[i]); }

which means that the code expects order[1] to be an array. It is, however, not an array; it's undefined, so you get the error. Why is it undefined? Well, your code doesn't do anything to make it anything else, based on what's in your question.

Now, if you just want to place a[i] in a particular property of the object, then there's no need to call .push() at all:

var order = [], stack = [];
for(var i=0;i<a.length;i++){
    if(parseInt(a[i].daysleft) == 0){ order[0] = a[i]; }
    if(parseInt(a[i].daysleft) > 0){ order[1] = a[i]; }
    if(parseInt(a[i].daysleft) < 0){ order[2] = a[i]; }
}

Solution 2

This error occurs in angular when you didn't intialise the array blank.
For an example:

userlist: any[ ];
this.userlist = [ ]; 

or

userlist: any = [ ];

Solution 3

order is an Object, not an Array().

push() is for arrays.

Refer to this post

Try this though(but your subobjects have to be Arrays()):

var order = new Array();

// initialize order; n = index
order[n] = new Array();

// and then you can perform push()
order[n].push(some_value);

Or you can just use order as an array of non-array objects:

var order = new Array();

order.push(a[n]);

Solution 4

In most cases you have to initialize the array,

let list: number[] = [];

Solution 5

I fixed in the below way with typescript

  1. Define and initialize firest

pageNumbers: number[] = [];

  1. than populate it

    for (let i = 1; i < 201; i++) {
        this.pageNumbers.push(i);
    }
    
Share:
492,332

Related videos on Youtube

d3nm4k
Author by

d3nm4k

Updated on February 12, 2021

Comments

  • d3nm4k
    d3nm4k about 3 years

    When pushing an array's contents to another array I get

    "Uncaught TypeError: Cannot read property 'push' of undefined" error in this snippet.

    var order = new Object(), stack = [];
    for(var i=0;i<a.length;i++){
        if(parseInt(a[i].daysleft) == 0){ order[0].push(a[i]); }
        if(parseInt(a[i].daysleft) > 0){ order[1].push(a[i]); }
        if(parseInt(a[i].daysleft) < 0){ order[2].push(a[i]); }
    }
    

    Why do I get this error in the second if statement ? Thanks a lot!

  • d3nm4k
    d3nm4k almost 10 years
    if it's defined as an object.. shouldn't i be able to use any number in there?
  • d3nm4k
    d3nm4k almost 10 years
    Even if i define it as an array, it still gives me the same error.
  • Pointy
    Pointy almost 10 years
    @d3nm4k no. You have to initialize order[0], order[1], and order[2] to have some value (an array I guess). That won't be done for you; why would it?
  • Pointy
    Pointy almost 10 years
    @d3nm4k do you just want to put the value of a[i] in either position 0, 1, or 2? If so, you don't use .push() for that. The .push() method is for adding values to the end of an array.
  • d3nm4k
    d3nm4k almost 10 years
    OH, got it! I would've spend a lot more hours just for getting order[1] = []; out. Thank You!
  • Aaron Jordan
    Aaron Jordan over 5 years
    If you want a safely typed array then, public _userList: User[] = []
  • Francisco A. Cerda
    Francisco A. Cerda over 2 years
    Thank you!!! No need to .push() at all, just assign.