Toolkit:array prototypes
From Surebert wiki
Overview
Surebert array prototypes making manipulating arrays much easier.
In Core Lib
Array.prototype.inArray
Checks to see if a value is contained in the array
javascript code
var myArray = [1,2,3]; var answer = myArray.inArray(2); //answer is true
Auto Loaded
Surebert array prototypes level the playing field for cross browser a javascript. If native javascript 1.5 array prototypes some, every, filter, forEach, indexOf, lastIndexOf, map are not defined - they will be loaded in via ajax. This allows you to use these native javascript array method in any browser.
Array.prototype.every
Checks to see if every value in an array returns true from the function provided
javascript code
function over10(val, key, arr) { if(val > 10){return true;} } var myArray = [5, 10, 15]; myArray.every(over10); //returns false because not every number in the array is over 10
Array.prototype.filter
Filters values out of an array that do not return true from the test function.
javascript code
function over10(val, key, arr) { if(val > 10){return true;} } var myArray = [5, 10, 15]; var newArray = myArray.filter(over10); //returns the array 10,15 because those two values are >=10
Array.prototype.forEach
Runs a function on every value in an array
javascript code
function addOne(val,key,arr){ val = val+1; } var myArray=[1,2,3]; myArray.forEach(addOne); //afterwards myArray = [2,3,4]
Array.prototype.indexOf
Finds the index of the value given within the array. Return the position of the first matching value. Rememeber that array start at 0.
javascript code
var myArray = [1,2,3,'a','b']; var answer = myArray.indexOf('a'); //answer is 3
Array.prototype.lastIndexOf
Finds the last index of the value given within the array. Rememeber that array start at 0.
javascript code
var myArray = [1,2,3,2]; var answer = myArray.lastIndexOf(2); //answer is 3
Array.prototype.map
Runs a function on every item in the array and returns the results in an array.
javascript code
function addTen(val, key, array) { return val+10; } var myArray = [5, 10, 15]; var answer = myArray.map(addTen); //answer = [15, 20, 25];
Array.prototype.some
If some of the function results are true then some returns true
javascript code
function isAboveFive(val, key, arr){ if(val >5) {return true;} } var myArray = [5, 10, 15]; var answer = myArray.some(isAboveFive); //answer = true //because some values return true when passed through the isAboveFive function
Externally Available
Additionally surebert has several array prototypes in the core lib and many more that can be loaded externally.
Array.prototype.avg
Used to determine the average value from an array of values
javascript code
var myArray = [1,3,4,5]; var average = myArray.avg(); average = 3.25
Array.prototype.copy
Makes a new independent copy of an array instead of a pointer to it
javascript code
var myNewArray = myArray.copy();
Array.prototype.empty
empties an array
javascript code
myArray.empty();
Array.prototype.inject
Finds the index of the value given within the array. Return the position of the first matching value. Rememeber that array start at 0.
javascript code
var myArray = ['zero', 'one', 'two']; var answer = myArray.inject(1, 'bagel'); //answer is now ['zero', 'bagel', 'one', 'two'];
Array.prototype.max
Finds the maximum value in an alpha/numeric array. Sorts alphanumerically and chooses the highest. Number have preference over letters, so 1 is higher than 'apple'
javascript code
var myArray = [5, 10, 15]; var answer = myArray.max(); //answer = 15;
Array.prototype.min
Finds the minimum value in an alpha/numeric array. Sorts alphanumerically and chooses the lowest. Numbers are higher than letters, so 'apple' is lower than 1
javascript code
var myArray = [5, 10, 15]; var answer = myArray.min(); //answer = 5;
Array.prototype.mostCommon
Finds the most common value in an array. If no value is most common then it returns false.
javascript code
var myArray = [5, 10, 15]; var answer = myArray.mostCommon(); //answer = false; var myArray = [5, 5, 10, 15]; var answer = myArray.mostCommon(); //answer = 5;
Array.prototype.natsort
Sort the array values in a natural alpha numeric way so that 1,10,2,3,4,5 becomes 1,2,3,4,5,10
javascript code
var myArray = [1,10,2,3,4,5]; var answer = myArray.natsort(); //answer = [1,2,3,4,5,10];
Array.prototype.random
Grab a random value from the array. The value is randomly selected each time the value is run.
javascript code
var myArray = [1,10,2,3,4,5]; var answer = myArray.random(); //answer = 4; //<--could change each time
Array.prototype.range
Determines the range of values in a numeric array. That is the highest value minus the lowest value
javascript code
var myArray = [1,10,2,3,4,5]; var answer = myArray.range(); //answer = 9; //<--the difference between 10 (the highest number) and 1 (the lowest number)
Array.prototype.reduce
This emulates Arrays.prototype.reduce from javascript 1.6 and is taken from the MDC site reference. .reduce() executes the callback function once for each element present in the array, excluding holes in the array, receiving four arguments: the initial value (or value from the previous callback call), the value of the current element, the current index, and the array over which iteration is occurring.
javascript code
myArray.reduce(function(previousValue, currentValue, index, array){ // ... }); //real examples var total = [0, 1, 2, 3].reduce(function(a, b){ return a + b; }); // total == 6 var flattened = [[0,1], [2,3], [4,5]].reduce(function(a,b) { return a.concat(b); }, []); // flattened is [0, 1, 2, 3, 4, 5]
Array.prototype.reduceRight
This emulates Array.prototype.reduceRight from javascript 1.6 and is taken from the MDC site reference. .reduceRight executes the callback function once for each element present in the array, excluding holes in the array, receiving four arguments: the initial value (or value from the previous callback call), the value of the current element, the current index, and the array over which iteration is occurring.
javascript code
myArray.reduce(function(previousValue, currentValue, index, array){ // ... }); //real examples var total = [0, 1, 2, 3].reduce(function(a, b){ return a + b; }); // total == 6 var flattened = [[0,1], [2,3], [4,5]].reduce(function(a,b) { return a.concat(b); }, []); // flattened is [0, 1, 2, 3, 4, 5]
Array.prototype.regex
Limit the values of an array to Values that do not match the regex expression are excluded
javascript code
var myArray = [5, 10, 15]; var answer = myArray.regex(/\d{2}/); //answer = [10,15] //because they are at least two digits as specified in the regex \d{2}
Array.prototype.remove
Removes a value or a set of values from an array. These are
javascript code
var myArray = [5, 10, 15]; var answer = myArray.remove([10,5]); //answer =[15]; var myArray = [6, 7, 8]; var answer = myArray.remove(6); //answer =[7,8];
Array.prototype.shuffle
Shuffle the values in an array randomly
javascript code
var myArray = [5, 10, 15]; myArray.shuffle(); //myArray =[10, 15, 5]; //<-could change each time
Array.prototype.sum
Add up the values in an array
javascript code
var myArray = [5, 5, 10, 15]; var answer = myArray.sum(); //answer =35;
Array.prototype.unique
Removes duplicate values from an array
javascript code
var myArray = [5, 5, 10, 15]; var answer = myArray.unique(); //answer =[5,10,15];
Array Iteration Lib
Iterating through array can be done with Array.prototype.iteration. This mini lib loads several Array.prototypes needed for iteration.
Array.prototype.pointer
Used to keep track of the array key we are referenceing with next, prev, end, current, rewind
Array.prototype.current()
Returns the array value of the key we are currently on as referenced by the pointer. Starts at 0 and chanegs base don use of next(), prev(), rewind(), end()
javascript code
var myArray = [1,10,2,3,4,5]; myArray.current(); //returns 1 at first //after manipulations it changes reference based on the pointer myArray.next(); //now it is the next one myArray.current(); //returns 10
Array.prototype.end()
Returns the array value of the last key and sets the array's pointer to that key
javascript code
var myArray = [1,10,2,3,4,5]; myArray.end(); //sets the pointer to the last array key and returns the value, in this case 5
Array.prototype.next()
Returns the array value of the last key and sets the array's pointer to that key
javascript code
var myArray = [1,10,2,3,4,5]; myArray.end(); //sets the pointer to the last array key and returns the value, in this case 5
Array.prototype.prev()
Returns the next value of the array and moves the pointer forward to it.
javascript code
var myArray = [1,10,2,3,4,5]; myArray.end(); //returns 5 myArray.prev(); //returns 4 myArray.current(); //returns 4
Array.prototype.rewind()
Returns the first value of the array and moves the pointer back to the beginning.
javascript code
var myArray = [1,10,2,3,4,5]; myArray.rewind(); //returns 1
Array.prototype.first()
Returns the first value in the array without moving the pointer.
javascript code
var myArray = [1,10,2,3,4,5]; myArray.first(); //returns 1
Array.prototype.last()
Returns the last value of the array without moving the pointer.
javascript code
var myArray = [1,10,2,3,4,5]; myArray.last(); //returns 5
Array.prototype.cycle()
Cycles through an array by incrememtning its pointer and reseting it back to the beginng (0) when it gets to the end.
javascript code
var myArray = [1,10,2,3,4,5]; var answer = myArray.cycle(); alert(myArray.cycle());