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, 2022 in Java by Soham
• 9,710 points
2,276 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, 2022 by Rahul
• 9,680 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, 2022 in Java by Aditya
• 7,680 points
5,851 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,230 points
3,067 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,230 points
3,470 views
0 votes
0 answers

Cannot read property 'push' of undefined when combining arrays

When pushing an array's contents to another ...READ MORE

Nov 16, 2022 in Java-Script by Ashwini
• 5,430 points
569 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,520 points
1,314 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,840 points
15,709 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,840 points
23,295 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, 2022 in Java by Rahul
• 9,680 points
2,095 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, 2022 in Java by Rahul
• 9,680 points
1,884 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP