I’m getting the error: object 0 has no method 'push', and I can’t figure out why.
I know that sack[i] is the object, i is 0 and quantity_to_spawn equals 1.
I think that node has an issue with pushing because sack is an array and sack[i] is actually an object.
for (i=0;i<rows[r].quantity_to_spawn;i++){
more_drops = Math.random()
sack[i]=new Array();
for (;more_drops > .05;){
more_drops = Math.random()
rarity = Math.random()
if (rarity <= .75&&typeof rows[r].common=="string"){//common drop 75%
item=rows[r].common.split(",")
sack[i].push(parseInt(item[parseInt(Math.random()*item.length)]))
...
I’m sure you are missing to declare the variable
sackas an array,or
Otherwise it should work
Here is the simple demo
I made some experiment regard to this problem, found some interesting facts. Those are,
The problem is
sackis already assigned something likevar sack = 'someValue';. in this case (assigned value string type), this resulting insackto be a string array. Hence the assignmentsack[i]=new Array();make no sense.sack[0]will bes. and try to push some value to this will throw the errorobject 0 has no method 'push'Another case(assigned value number type), assignment is like
var sack = 28892;. In this case, the same array assignment making no sense. But if you try to push something tosack[0]it will throwCannot call method 'push' of undefined, sincesack[0]isundefined.In both cases, after declaring
sackto some value, the assignment not produced any error, though it is useless.Additional information about array declaration,
Javascript array declaration: new Array(), new Array(3), ['a', 'b', 'c'] create arrays that behave differently