86

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!

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
d3nm4k
  • 1,023
  • 1
  • 8
  • 12

10 Answers10

81

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]; }
}
Pointy
  • 405,095
  • 59
  • 585
  • 614
78

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

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

or

userlist: any = [ ];
Thilina Koggalage
  • 1,044
  • 8
  • 16
Yogesh Aggarwal
  • 994
  • 7
  • 9
30

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]);
Community
  • 1
  • 1
Kyle Emmanuel
  • 2,193
  • 1
  • 15
  • 22
  • 1
    Even if i define it as an array, it still gives me the same error. – d3nm4k Jul 19 '14 at 14:39
  • @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. – Pointy Jul 19 '14 at 14:41
17

In most cases you have to initialize the array,

let list: number[] = [];
Akitha_MJ
  • 3,882
  • 25
  • 20
4

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);
    }
    
Shahid Hussain Abbasi
  • 2,508
  • 16
  • 10
3

Generally, Push method is used to add elements at the end of an array.

Here, you have used the push method to an object and not an array which is 'order'.

Steps to debug...

Change the object into an empty array,

var order = [], stack = [];

Now you can use the push method for this array as usual.

To use push method to this 'order' array; you should not use the array index when calling push method to an array. Just use the name of the array only.

var order = [], stack = [];

for(var i = 0; i<a.length; i++){

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

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

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

You do not need to give an index.

Instead of doing order[0].push(a[i]), just do order.push(a[i]).

CyanCoding
  • 1,012
  • 1
  • 12
  • 35
Manish
  • 159
  • 2
  • 16
1

answer to your question is simple order is not a object make it an array. var order = new Array(); order.push(/item to push/); when ever this error appears just check the left of which property the error is in this case it is push which is order[] so it is undefined.

Vishal Patil
  • 381
  • 3
  • 15
0

order[] is undefined that's why

Just define order[1]...[n] to = some value

this should fix it

Jimma
  • 1
0

Make sure add logical AND (&&) before.

let users = [{id: 1, name: 'amoos'}, {id: 2, name: 'rifat'}]
let newUser = {id: 3, name: 'someone'} 
    users && users.push(newUser)    
    console.log('After', users)
GMKHussain
  • 3,342
  • 1
  • 21
  • 19