Associative Arrays

Associative arrays are defined as any other array is defined, but strings are inserted in place of numbers as indexes. This solution allows us to call an item in an array by name rather than by an index number, which obviously makes it easier to target specific items. This can be useful when trying to replicate a hash map of sorts, which is not a native data type in JavaScript. Here is a sample of the syntax for this option.

var employee = new Array();
employees["firstName"] = "Kris";
employees["lastName"] = "Hadlock";

As you can see, this option may make it easier to target specific items, but again, imagine having to create multiple employees. Here is an example of how unruly this could become.

var employee = new Array(); ...

Get Ajax for Web Application Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.