Associative arrays in Javascript

Associative arrays in Javascript

If you programmed in PHP, you are probably familiar with associative arrays. Associative arrays allow strings to be used as indices, not just numbers.

If you programmed in PHP, you are probably familiar with associative arrays. Associative arrays allow strings to be used as indices, not just numbers. Example:

var SE = new Array();

SE ['one'] = 1;

SE ['two'] = 2;

None of the methods of the Array class allows you to display elements of an associative array. The length property also does not work, so you cannot iterate over the elements of an associative array in a for loop. To do this, use the for ..in loop:

for (var SP in SE) {

  document.write(SP + " = " + SE[SP] + "<br>");

}

The output will be:

one = 1

two = 2