Take the 2-minute tour ×

OK I am looping through the properties in an object like so:

private var _propsList:Object = {'Type':'product_type'
                        ,'Stone Color':'primary_stone_sub'

for(key in _propsList)
    val = _propsList[key];
    trace(key +" = "+ val);

I am expecting the first trace to be Type = property_type since that is the first one defined in the array, however it is coming up random everytime. I guess this is because my keys are strings and not integers, however is there a way to specify the order it loops through them?


share|improve this question

4 Answers 4

up vote 4 down vote accepted

You can't rely on for (v in someObject) ... to return things in a predictable order, no.

Depending on your specific situation, you could just use an array to hold the keys, and just iterate through that:

private var keys:Array = ["Type", "Kind", "Stone", "Stone Color", "Metal", "Brand"];

private function iterate():void
    for each (var k:String in keys)

Maybe a bit obvious or non-elegant, but it'd get the job done. :)

share|improve this answer

I found this link that gives some background: Subtle Change in for..in Loops for ActionScript 3

This question is actually a dup of this one.

share|improve this answer

In general, I think this is a case where you shouldn't depend on a particular behavior from the framework/language you are using. This type of behavior is generally poorly documented and can change from version to version.

If you really need a specific retrieval order, I would create a wrapper class as jevinkones suggested above. Maybe there's even a utility class in the framework somewhere to accomplish this (Dictionary, etc.?)

HTH, Karthik

share|improve this answer

you could hack it by classing-out your "_propsList" object creating an array inside of the newly created PropsList class that references the properties in order. At that point, you could run a FOR loop on the array and get your properties in order.

OR, you could have a function inside that new class that would return an Array of those properties. like this:

public function getProps():Array {
    return [myPropertyOne, myPropertyTwo, myPropertyThree];
share|improve this answer

Your Answer


