Cannot read property 'push' of undefined when combining arrays

JavascriptJqueryWebConsole

Javascript Problem Overview


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!

Javascript Solutions


Solution 1 - Javascript

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

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

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

In most cases you have to initialize the array,

let list: number[] = [];

Solution 5 - Javascript

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);
     }
    

Solution 6 - Javascript

You do not need to give an index.

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

Solution 7 - Javascript

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]); 
   }
}

Solution 8 - Javascript

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.

Solution 9 - Javascript

order[] is undefined that's why

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

this should fix it

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiond3nm4kView Question on Stackoverflow
Solution 1 - JavascriptPointyView Answer on Stackoverflow
Solution 2 - JavascriptYogesh AggarwalView Answer on Stackoverflow
Solution 3 - JavascriptKyle EmmanuelView Answer on Stackoverflow
Solution 4 - JavascriptAkitha_MJView Answer on Stackoverflow
Solution 5 - JavascriptShahid Hussain AbbasiView Answer on Stackoverflow
Solution 6 - JavascriptManishView Answer on Stackoverflow
Solution 7 - JavascriptGowtham ReillyView Answer on Stackoverflow
Solution 8 - JavascriptVishal PatilView Answer on Stackoverflow
Solution 9 - JavascriptJimmaView Answer on Stackoverflow