简体   繁体   中英

Objects as Associative Arrays: for loop

Is it possible to loop through a data set of objects as associative arrays?

I have a bunch of JSON data, and would like to loop through all of the data sets and pull out a property in each object.

for example:

for ( var i = 0; i <= 20; i++){
var oblivion = i;
var myObject = new MYobject( oblivion);
oblivionLoader(myObject);
}


function oblivionLoader(myObject)
{
 for ( i = 1; i<=2; i++)
 {
   var changer = myObject.oblivion[i];
   var infoText = GetDetailsText(changer);
   infoText.html(myObject.toString());
 }
}

If this is possible please show me how. Otherwise I am concluding it is impossible...

you can use a for in loop to loop through properties of an object.

var myObject = { prop1:"1", prop2:"2", prop3:"3" }, 
    property;

for ( property in myObject ) {
    if ( myObject.hasOwnProperty( property ) { 
        alert( myObject[property] );
    }
}

the bracket and dot syntax is interchangeable in JavaScript.

That being said, I have no idea what you're trying to do in you're example...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM