8. Associative Arrays
Associative arrays are arrays with name indexes.
Many programming supports this. However, in JavaScript, named indexes are not
supported in arrays. Arrays in JavaScript always uses numbered indexes. Take
note that arrays in JavaScript can contain an array with name indexes.
For example,
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p id="demo"> </p>
<script>
var person = ["Jack", "Sawyer", {age:28, country:"Philippines"}];
document.getElementById("demo").innerHTML = person[2]["country"];
</script>
</body>
</html>
In the example above, it will display “Philippines”. We cannot directly access “Philippines”. Instead, we need to access the element with index 2. This element is an object type. In order to access the members of this object, the named index is used. Thus, the code person[2][“country”].
Aside from using the square bracket ([]) property accessor, element or item in an object can be accessed using the dot (.) property accessor. For example, to access the element “Philippines”, you can use the code person[2].country.
For more information on the differences between the dot notation and the bracket notations in accessing object items, please visit:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors