[turboid] javascript solutions

Home     Contact     Search     About me
Arrays
05 Apr 2012

Better array functions for JavaScript

Easier manipulation of arrays

Whoever works a lot with arrays in different ways will after some time discover that standard JavaScript is a little bit poor in terms of array treating functions. This is why this article introduces you to the array functions provided by Turboid, which makes your life with arrays much easier.

Here is a nice collection of functions which allow you to drop and insert elements in an array where you want and by the criteria you want. Especially associative arrays are now much easier to handle. A special feature are the pointer functions which let you walk through an array without forgetting where you have been in it at your last made step.

[Array].drop(index)

Deletes an element at an array index of your choice. This is easier and more flexible than standard functions like .pop(), .shift() or .slice(). Returns the modificated array.


[Array].dropByValue(value)

Deletes an element by value instead of deleting it by index. All elements containing value are affected. Returns the modificated array.


[Array].insertAt(i, val)

Inserts a new element at index i, containing the value "val".


[Array].contains(value)

Returns whether the array contains an element containing the passed value. Usage example: if(myList.contains("Herbert"))... Not for associative arrays.


[Array].getIndex(value)

Returns the index of the first element containing value.


[Array].avg()

Gets the average value of all numbers in the array.


[Array].getValueAtPoint(index)

Although associative arrays officially don't work with indexes, this function informs you about the value which is saved at the passed index, solely based on the chronological order which the values of the associative array have been saved with.


[Array].getKeyAtPoint(index)

The same as .getValueAtPoint(index), providing the key name instead of the value.


[Array/Object].getLength()

Returns the number of elements in an associative array.


getKeyOfPeak(Array/Object)

Returns the key of the highest value in an array or object


getKeyOfLowest(Array/Object)

Returns the key of the lowest value in an array or object


valuesort(toBeSorted [, -1])

Sorts an array (value) by value. The optional argument -1 lets the array be sorted setting the highest value as first element and the lowest value as the last element.


keysort(toBeSorted)

Sorts an assoiciative array by its key names.


hasArrayElements(var)

Checks if var is a real array already containing elements.


[Array].getNext()

Lets you walk step by step through an array, returning the value of each next element, beginning with index 0. Each time the function is used, its pointer is set on the next index. Returns null after passing the last element.


[Array].setNext(next)

Lets the pointer of .getNext() jump to next.


[Array].getPrev()

Like .getNext(), but always returning the next previous element.


[Array].getCurrent()

Gets the current element without changing the pointer.


_(val).isArray()

Checks if val is an array or not.


ignoreUC

Prevents the standard JavaScript array function .sort() dividing words written in upper case and words written in lower case in two different groups. Usage: [Array].sort(ignoreUC)


In order to use these extra functions, simply download the free Turboid framework and put it into the directory of your HTML file, then write this line into the head section of your HTML file, and that's it:

	

Alternatively, you can use the smaller Turboid core along with Turboid varTools if you don't want to download the whole framework.


Comments

Add comment:

Name: (required)
E-Mail: (required, remains invisible)
Website:
Comment:
Please check all (anti-spam):