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 sack
as an array,
var sack = new Array();
or
var sack = [];
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 sack
is already assigned something like var sack = 'someValue';
. in this case (assigned value string type), this resulting in sack
to be a string array. Hence the assignment sack[i]=new Array();
make no sense. sack[0]
will be s
. and try to push some value to this will throw the error object 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 to sack[0]
it will throw Cannot call method 'push' of undefined
, since sack[0]
is undefined
.
In both cases, after declaring sack
to some value, the assignment not produced any error, though it is useless.
Additional information about array declaration,
No idea what you are doing here, but try this:
var sack = [];
for (var i=0;i<rows[r].quantity_to_spawn;i++) {
var more_drops = Math.random();
sack[i] = [];
for (;more_drops > 0.05;) {
more_drops = Math.random();
var rarity = Math.random();
if (rarity <= 0.75&&typeof rows[r].common==="string") {//common drop 75%
var item = rows[r].common.split(",");
sack[i].push(parseInt(item[parseInt(Math.random()*item.length,10)],10));
...