Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

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

for ( propertyin 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...

Post a Comment for "Objects As Associative Arrays: For Loop"