Additional functions on the JS Array object
| Array | Additional functions on the JS Array object |
| Functions | |
| every | Tests whether all elements in the array pass the test implemented by the provided function. |
| filter | Creates a new array with all elements that pass the test implemented by the provided function. |
| forEach | Executes a provided function once per array element. |
| indexOf | Returns the first index at which a given element can be found in the array, or -1 if it is not present. |
| lastIndexOf | Returns the last index at which a given element can be found in the array, or -1 if it is not present. |
| map | Creates a new array with the results of calling a provided function on every element in this array. |
| dim | returns an array of size count containing null objects |
| reduce | Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value. |
| reduceRight | Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value. |
| some | Tests whether some element in the array passes the test implemented by the provided function. |
| min | returns the “smallest” member of the array. |
| max | returns the “largest” member of the array. |
| sum | returns a sum of the array. |
| avg | returns an average of the array. |
| last | returns the last value in this array |
| first | returns the first value in this array |
| parse | Static function that returns an array from an array like object, or null if conversion is not possible |
| compact | removes undefined values from an array |
| contains | searches an array and returns true if one or more are found |
| appendUnique | adds a new element to this array, if that element is not already in this array. |
if ( !Array.prototype.every )
Tests whether all elements in the array pass the test implemented by the provided function.
https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /every
if ( !Array.prototype.filter )
Creates a new array with all elements that pass the test implemented by the provided function.
https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /filter
if ( !Array.prototype.indexOf )
Returns the first index at which a given element can be found in the array, or -1 if it is not present.
if ( !Array.prototype.lastIndexOf )
Returns the last index at which a given element can be found in the array, or -1 if it is not present.
if ( !Array.prototype.map )
Creates a new array with the results of calling a provided function on every element in this array.
https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /map
if ( !Array.dim )
returns an array of size count containing null objects
| count | number of integers in returned array |
var strings = Array.dim(5).map(function(element,index){
return "String " + index++;
})
if ( !Array.prototype.reduce )
Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /reduce
if ( !Array.prototype.reduceRight )
Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
if ( !Array.prototype.some )
Tests whether some element in the array passes the test implemented by the provided function.
https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /some
Array.prototype.min = function( compare )
returns the “smallest” member of the array.
| compare | Optional, default: function(a,b){return a < b} A compare function like sort() uses to determine the minimum value |
Array.prototype.max = function( compare )
returns the “largest” member of the array.
| compare | Optional, default: function(a,b){return a > b} A compare function like sort() uses to determaxe the maximum value |
Array.prototype.sum = function( accessor )
returns a sum of the array.
| accessor | Optional, default: function(element){return element} A function that takes an element of the array and returns a value to be summed. This is useful to force integer math or to sum a property of the objects in the array rather than the objects themselves. |
Array.prototype.avg = function( accessor )
returns an average of the array.
| accessor | Optional, default: function(element){return element} A function that takes an element of the array and returns a value to be averaged. This is useful to force integer math or to average a property of the objects in the array rather than the objects themselves. Note: null values are ignored. If you want to count nulls as 0, use this accessor |
function(element){
return element===null?0:element;
}
Array.prototype.last = function()
returns the last value in this array
| defaultValue | Optional, default throws error If defined, this value will be returned if the Array is empty. Otherwise an error is thrown |
Array.prototype.first = function()
returns the first value in this array
| defaultValue | Optional, default throws error If defined, this value will be returned if the Array is empty. Otherwise an error is thrown |
Array.parse = function ParseArray( obj, accessFunction, lengthFunction )
Static function that returns an array from an array like object, or null if conversion is not possible
| obj | object to convert |
| accessFunction | Optional, default function(obj,index){return obj[index]} function that takes obj and an index and returns the item at that index |
| lengthFunction | Optional, default function(obj){return obj.length} function that takes obj and returns the length of the collection |
Takes an array-like object and returns an array containing its items.
//convert function arguments into an array of arguments
function echoArgs(){
Myna.printDump(Array.parse(arguments))
}
// client side example:
// getElementsByTagName() returns an array-like collection, but it doesn't
// have any of the Array extras like "filter"
debug_window(
Array.parse(document.getElementsByTagName("div"))
.filter(function(div){
return /panel/.test(div.className);
})
)
Built-in handling exisits for E4X nodes, such that these are valid syntax:
var array = Array.parse(xml.entry.title)
var array = Array.parse(xml..*)
Array.prototype.compact = function compactArray()
removes undefined values from an array
Modifies an array in place by removing values and renumbering the indexes
//create an array with 50 null values
var a=Array.dim(50);
// now delete half of them
Array.dim(25).forEach(function(d,i){
delete a[i];
})
Myna.println(a.length);//prints 50
//now compact
a.compact();
Myna.println(a.length);//prints 25
Array.prototype.contains = function contains( search )
searches an array and returns true if one or more are found
| search | a simple value or a function to identify a matching entry. If this is function, it will be called with a the current array item and should return true if this is a matching item |
unlike indexOf This function does a loose comparison. This means that if, for instance you search for false, you will match entries with values of null, undefined, “”,0,etc
//does the array contain "woot" ?
return someArray.contains("woot");
//does the array contain an entry that contains "woot" ?
return someArray.contains(function(entry){
return /woot/.test(entry)
});
//does the array contain "woot" and "dude"?
return ["woot","dude"].every(someArray.contains)
//does the array contain an entry that contains "woot" OR "dude"?
return ["woot","dude"].some(function(term){
return someArray.contains(function(entry){
return new RegExp(term).test(entry);
})
})
Array.prototype.appendUnique=function( val, looseCheck )
adds a new element to this array, if that element is not already in this array.
| val | value to append |
| looseCheck | Optional, default false Normally this function uses the “same in type and value” operator (===). Setting this argument to true will causes this function to use the looser, and slower “same in value” operator (==) |
val
Tests whether all elements in the array pass the test implemented by the provided function.
if ( !Array.prototype.every )
Creates a new array with all elements that pass the test implemented by the provided function.
if ( !Array.prototype.filter )
Executes a provided function once per array element.
if ( !Array.prototype.forEach )
Returns the first index at which a given element can be found in the array, or -1 if it is not present.
if ( !Array.prototype.indexOf )
Returns the last index at which a given element can be found in the array, or -1 if it is not present.
if ( !Array.prototype.lastIndexOf )
Creates a new array with the results of calling a provided function on every element in this array.
if ( !Array.prototype.map )
returns an array of size count containing null objects
if ( !Array.dim )
Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
if ( !Array.prototype.reduce )
Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
if ( !Array.prototype.reduceRight )
Tests whether some element in the array passes the test implemented by the provided function.
if ( !Array.prototype.some )
returns the “smallest” member of the array.
Array.prototype.min = function( compare )
returns the “largest” member of the array.
Array.prototype.max = function( compare )
returns a sum of the array.
Array.prototype.sum = function( accessor )
returns an average of the array.
Array.prototype.avg = function( accessor )
returns the last value in this array
Array.prototype.last = function()
returns the first value in this array
Array.prototype.first = function()
Static function that returns an array from an array like object, or null if conversion is not possible
Array.parse = function ParseArray( obj, accessFunction, lengthFunction )
removes undefined values from an array
Array.prototype.compact = function compactArray()
searches an array and returns true if one or more are found
Array.prototype.contains = function contains( search )
adds a new element to this array, if that element is not already in this array.
Array.prototype.appendUnique=function( val, looseCheck )