0

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)]))
 ...
franzlorenzon
5,9216 gold badges39 silver badges58 bronze badges
asked Aug 15, 2012 at 4:28
5
  • Are you declaring sack before the outer for loop as an array? Commented Aug 15, 2012 at 4:31
  • I'd be more comfortable if I saw a few var keywords in there... Commented Aug 15, 2012 at 4:32
  • 1
    Use semicolons, they should not be optional. Why does it seem everything is also global? Commented Aug 15, 2012 at 4:34
  • 1
    Also your random times length is going to give issues when Random returns one. Commented Aug 15, 2012 at 4:41
  • Add var sack = []; before the first for. Commented Aug 15, 2012 at 4:50

2 Answers 2

1

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,

Javascript array declaration: new Array(), new Array(3), ['a', 'b', 'c'] create arrays that behave differently

answered Aug 15, 2012 at 5:19
Sign up to request clarification or add additional context in comments.

3 Comments

This can't be the issue. If it was, there would be sack is undefined thrown just before the second for loop.
@Teemu no, what will happen if the variable defined as var sack;, obviously there would be no such error.
In that case the error would be Can't set property 0, object is null or undefined in line sack[i]=new Array();. There's no way to execute code past this line, if sack is not an array. OP's having this error: object 0 has no method push, this tells that object (=== sack[0]) exists, but there's not method called push in that object.
0

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));
 ... 
answered Aug 15, 2012 at 5:40

1 Comment

This can't be the issue. If it was, there would be sack is undefined thrown just before the second for loop.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.