-1

OK, I am trying to access an object in a loop for my code to work in every object, unfortunately, my object is located in another object. example:

var object = new Object();
object.insider1 = new Object();

object.insider1.name = "ex";
object.insider1.type = "blah";

object.insider2 = new Object();

object.insider2.name = "Ex2";
object.insider2.type = "blah2";

Now to access it with the loop:

for(var g=0; g<object[object.length]; g++){
//do stuff
}

object[object.length] is being noted as 'undefined' and therefore I can't use it... Is there any way to fix this? Thank you!

4

1 回答 1

2

Looks to me that what you really want is an array property to hold your "insiders". I would do:

var object = new Object(); // or simply {}
object.insiders = [];
object.insiders.push({name: "ex", type: "blah"}); 
object.insiders.push({name: "Ex2", type: "Blah2"});
// Or object.insiders.push(insider1) if you have created them already

for(var i = 0; i < object.insiders.length; i++) {
    // Do stuff
}
于 2013-04-01T21:22:34.190 回答