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(); employees[001]["firstName"] = "Kris"; employees[001]["lastName"] = "Hadlock"; employees[002]["firstName"] = "John"; employees[002]["lastName"] = "Doe";


This solution can become a real mess if you are working with a lot of data and having to remember all of the paths to certain items in the array. With that said, using this method to create a custom hash map object could be very useful, but I'll leave that part up to you.



Ajax for Web Application Developers
Ajax for Web Application Developers
ISBN: 0672329123
EAN: 2147483647
Year: 2007
Pages: 129
Authors: Kris Hadlock

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net