Cannot read property push of undefined when combining arrays

0 votes

When pushing an array's contents to another array I got the following ERROR:-

"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 ? Any help would be appreciated, thanks a lot!

Feb 11 in Java by Soham
• 8,730 points
19 views

1 answer to this question.

0 votes

The reason why you get this ERROR is due to the reason that the order[1] is undefined. In other words, there has been an attempt made somewhere in your code to access a property with a name such as in this example it is the word “push” and however, apart from using the object, the base of the reference is undefined and thus, you will have to look for code that refers “push” and check what it has left of it. It is mentioned below for this case:- 

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

This will mean that the code expects order[1] to be an array. But, however, not an array; it's undefined, so you get the error. Why is it undefined? It is due to the reason that 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]; } 
}

answered Feb 11 by Rahul
• 8,980 points

Related Questions In Java

0 votes
1 answer

Uncaught TypeError: Cannot read property 'value' of null

As you have not shared your HTML ...READ MORE

answered Feb 22 in Java by Aditya
• 7,280 points
416 views
0 votes
1 answer
0 votes
2 answers

How can I invoke a method when the method name is in the form of a given string?

You could probably use method invocation from reflection: Class<?> ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,170 points
1,019 views
0 votes
2 answers

What is the use of @Override annotation in Java ? When do we use it ?

@Override annotation is used when we override ...READ MORE

answered Aug 14, 2019 in Java by Sirajul
• 59,170 points
1,566 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,510 points
284 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'msie' of undefined - jQuery tools

Hello, Use the following script tag in your ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,680 points
7,935 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,680 points
14,571 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,680 points
1,091 views
0 votes
1 answer

Failed to load resource: the server responded with a status of 404 (not found)

In order to avoid an error while ...READ MORE

answered Feb 8 in Java by Rahul
• 8,980 points
44 views
0 votes
1 answer

Using setTimeout to delay timing of jQuery actions

To answer your question, the .html() only ...READ MORE

answered Feb 9 in Java by Rahul
• 8,980 points
76 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP