Tag: associative arrays

Associative Arrays

If you’ve used javascript much, you are probably familiar with arrays. Typically arrays are used to manage lists of items. I frequently use arrays for maintaining information about the state of pages and quiz questions in learning modules, storing a series of properties for each item.

But, there is a problem. To find the particular item you want to use, you must loop through the whole array. Sometimes you can get yourself into a situation requiring multiple loops to get one item of information and performance will suffer.

I recently got to wondering if there weren’t a way to jump straight to the desired item, if the ID of the item were known. Yes there is!

Enter “associative arrays“. Associative arrays allow you to index items by any string key, not just the usual arbitrary index numbers. So you can go straight to the exact item by name without testing for property matches while looping through the array.

Continue reading