loop through object properties
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: loop through object properties

  1. #1
    Senior Member
    Join Date
    Apr 2001
    Posts
    996

    loop through object properties

    I have an object that gets properties added in this sequence.

    Home
    School
    living
    status
    sound
    Memory

    When I loop through the object they don't come out in that sequence.

    data is the object

    for (var i:String in data)
    {
    trace(i + ": " + data[i]);
    }
    Is there a way to sort it maybe?

  2. #2
    Junior Member
    Join Date
    Apr 2007
    Posts
    9
    This link should show you how to Display (trace, or whatever) the properties:
    http://www.negush.net/blog/listing-t...-of-an-object/

    What you were doing wrong was that data[i] was NOT accessing the ith property of data.

    to access a property of an object, I would recommend you use a period, for example:

    data.Home, data.School, etc...

    But if you really like the square parenthesis, you would have to use

    data['Home'], data['School'], etc...

    Notice the hyphens, ( ' ), make it so the stuff in between is a String (or possibly a label, not sure if there is such a thing or if there is a difference). My guess is that doing data[i] was actually converting the Number i into a String (basically, using the ASCII table). So it might be possible to access the properties in an object using a loop like that, but you would have to do something like:

    data's properties: prop1, prop2, prop3...

    for (var i:uint = 1; i < Last_Prop_Number; i++){
    trace(i + ": " + data['prop'+String(i)]);
    }

    and with any luck, 'prop'+String(i) will become 'prop1', 'prop2', 'prop3'...

    Of course, this means you have to RENAME ALL of your properties so they have these meaningless names, which if you were going to do that you might as well use an array:

    var data:Array = [];
    data[0] = Home;
    data[1] = School;
    //...

    and then the for loop will work for sure.

    Hope that helps

  3. #3
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    756
    This issue has to do with the way the AVM works which was built in C++. Basicly there is a hash table that stores the information in an unordered map.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com