iArray is a JavaScript class to facilitate some basic level of associative arrays.
Associative arrays are data arrays in which the are associated with a key value rather than just a numeric index. Many languages such as PHP already support such data types. In effect it allows you to keep an array of arbitary data objects accessible by a name (the key) rather than having to store (and keep current following deletion or insertion) the index.
Index-Based Array Associative Array
0 = Hello first = Hello
1 = There second = There
2 = World third = World
In this example we would refer to 'There' in the index array by calling the index(1) and in the associative array with the key value 'second'.
The iArray object allows you to refer to an element by index or key value as shown in the documentation.
Anything not covered here or problems? Post to the PurplePixie Support Forum (under "Anything Else")
Using iArray
To use iArray simply download the code and include in your project (or hotlink) as described on the Code/Download page (link at the top).
Then just create an iArray object with something like: var myarray = new iArray();
You can then add/fetch/find/delete elements from the array using the provided methods (detailed in full below).
For example: myarray.set('mykey','some value');
Would set an array element with the key 'mykey' to be 'some value' (if that key doesn't already exist then it would be appended to the end of the array otherwise the value would be updated).
A Working Example
You can see the output generated by the below example by clicking here
Code from this example:
iArray Methods
iArray.set(key, value)
Sets an array element of specified key to value. If element doesn't exist it is created (using iArray.push) otherwise the value is updated.
Unless you are CERTAIN the key doesn't exist then you should use this rather than iArray.push as the overhead is not much and it's duplicate key safe.
iArray.get(key)
Gets the value of the element associated with the key (returns false if not found but of course the value could be false so if not sure check with iArray.exists first).
iArray.unset(key)
Delete (unset) the element associated with key (and the key), returns false if failure (doesn't exist) or true on success.
We use unset rather than delete because delete will break MSIE.
iArray.exists(key)
Check if the key exists and returns true if it does or false if not found.
iArray.push(key, value)
Adds element to the end of the array with key and value specified. See notes in iArray.set as to why you should use iArray.set instead.
iArray.length()
Returns the length of the array (note this is a METHOD and not a property as with standard JS arrays e.g. you need the () after it)
iArray.getIndex(key)
Returns the index of the element associated with the provided key or -1 if the key is not found.
iArray.getKeyByIndex(index)
Get the key for the array element at given index position
iArray.getValByIndex(index)
Get the value for the array element at given index position
Examples
var myarray = new iArray(); // create the iArray object
myarray.set('fish','sea'); // set an element key=fish value=sea
myarray.set('lizard','land'); // set an element key=lizard value=land
myarray.set('fish','Sea'); // update element with key=fish to value=Sea (upper case S)
var fish_live_in_the = '';
if (myarray.exists('fish))
fish_live_in_the = myarray.get('fish'); // get value for key 'fish' if exists
else
fish_live_in_the = "no idea"; // otherwise we have no idea
myarray.unset('fish'); // enough of the fish!