Question: I have a JavaScript object that is treated as an associative array. Let's call it "fields". It has several elements, e.g.:

```
fields['element1'] = ...
fields['element2'] = ...
fields['element3'] = ...

```

Given fields[0], is it possible to obtain the name of the property (which is "element1") instead of its value?

Answer: 
Let's say you have an object oObject. It could be:

```
var oObject = {} ;
oObject["aaa"] = "AAA" ;
oObject["bbb"] = "BBB" ;
oObject["ccc"] = "CCC" ;
oObject["ddd"] = "DDD" ;
oObject["eee"] = "EEE" ;

```

Now, let's say you want to know its properties' names and values, to put into the variable strName and strValue. For that you use the "for(x in o)" construct, as in the following example:

```
var strName, strValue ;

for(strName in oObject)
{
   strValue = oObject[strName] ;
   alert("name : " + strName + " : value : " + strValue) ;
}

```

The "for(x in o)" construct will iterate over all properties of an object "o", and at each iteration, will put in variable "x" the current property name. All you have to do, then, to have its value, is to write o[x], but you already knew that.

Additional info
---------------

After some thinking, and after seeing the comment of Hank Gay, I feel additional info could be interesting.

Let's be naive (and forget the "in JavaScript, all objects, including arrays, are associative containers" thing).

You will usually need two kind of containers: Maps and Arrays.

Maps are created as in my example above (using the "o = new Object() ;" or the "o = {} ;" notation, and must be accessed through their properties. Of course, maps being maps, no ordering is guaranteed.

Arrays are created differently, and even if they can be accessed as maps, they should be accessed only through their indices, to be sure order is maintained.

Point is:

* If you need a map, use a "new Object()" container
* If you need an array, une an array, use a "new Array()" container
* Don't EVER mix the two, and don't EVER access the map through indices, and for arrays, ALWAYS access its data through its indices, because if you don't follow those principles, you won't get what you want.
No, for two reasons.

1. fields[0] and fields["element1"] are different properties.
2. properties in an object are explicitly unordered

You could loop over the properties:

```
function (obj) {
  for (prop in obj) {
    if (obj.hasOwnProperty(prop) {
      return prop;
    }
  }
};

```

…to get the "first" property for some arbitrary value of "first" that could change at any time.

<http://ajaxian.com/archives/fun-with-browsers-for-in-loop> explains the hasOwnProperty pattern.