Cannot read property 'push' of undefined when combining arrays
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
- Define and initialize firest
pageNumbers: number[] = [];
than populate it
for (let i = 1; i < 201; i++) { this.pageNumbers.push(i); }
Related videos on Youtube
d3nm4k
Updated on February 12, 2021Comments
-
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 almost 10 yearsif it's defined as an object.. shouldn't i be able to use any number in there?
-
d3nm4k almost 10 yearsEven if i define it as an array, it still gives me the same error.
-
Pointy almost 10 years@d3nm4k no. You have to initialize
order[0]
,order[1]
, andorder[2]
to have some value (an array I guess). That won't be done for you; why would it? -
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 almost 10 yearsOH, got it! I would've spend a lot more hours just for getting order[1] = []; out. Thank You!
-
Aaron Jordan over 5 yearsIf you want a safely typed array then,
public _userList: User[] = []
-
Francisco A. Cerda over 2 yearsThank you!!! No need to .push() at all, just assign.