0

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...

5
  • Is it your intention to overwrite the preceding myObject in the 1st loop? Commented Jul 15, 2011 at 15:19
  • 2
    what is MYobject and i don't see an array. Commented Jul 15, 2011 at 15:19
  • I did this example in a hurry without thinking. Basically I need to grab all of the data, and pass it through one at a time into a function that loops through the data. Commented Jul 15, 2011 at 15:24
  • Imagine making a timed display of data on a screen based on looping through this object is essentially my goal. 90% of everything is written correctly, but I am having an issue looping through this objects properties. Commented Jul 15, 2011 at 15:26
  • your example code is very confusing; my best guess is that you're looking for for..in loops - see eg stackoverflow.com/questions/587881/… Commented Jul 15, 2011 at 15:30

1 Answer 1

3

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...

Sign up to request clarification or add additional context in comments.

3 Comments

you're missing a var declaration for property (assuming you don't want it to be global...); also, I recommend checking hasOwnProperty() as well...
How would you grab a paticular order in the my object loop? Since the loop goes 21 times. I how do I grab the object from the 13 loop?
I don't think you understand the difference between arrays and objects in JavaScript, take a look at this stackoverflow.com/questions/874205/…

Your Answer

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.