Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Getting a name of an element in associative array

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?

like image 876
Ariod Avatar asked Aug 07 '09 10:08

Ariod


People also ask

What is an associative array include syntax?

An associative array is an array with string keys rather than numeric keys. Associative arrays are dynamic objects that the user redefines as needed. When you assign values ​​to keys in a variable of type Array, the array is transformed into an object, and it loses the attributes and methods of Array.

Does JavaScript support associative array?

JavaScript does not support associative arrays. You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers.

What is associative array in JavaScript?

Associative arrays are basically objects in JavaScript where indexes are replaced by user defined keys. They do not have a length property like normal array and cannot be traversed using normal for loop. Following is the code for associative arrays in JavaScript −


2 Answers

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.
like image 87
paercebal Avatar answered Sep 28 '22 07:09

paercebal


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.

like image 36
Quentin Avatar answered Sep 28 '22 08:09

Quentin