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. |
| 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. |
| 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 |
Array.prototype.every = function( fun /*, thisp*/ )
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
Array.prototype.filter = function( fun /*, thisp*/ )
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
Array.prototype.forEach = function( fun /*, thisp*/ )
Executes a provided function once per array element.
Array.prototype.indexOf = function( elt /*, from*/ )
Returns the first index at which a given element can be found in the array, or -1 if it is not present.
Array.prototype.lastIndexOf = function( elt /*, from*/ )
Returns the last index at which a given element can be found in the array, or -1 if it is not present.
Array.prototype.map = function( fun /*, thisp*/ )
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
Array.dim = function( count )
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
Array.prototype.reduce = function( fun /*, initial*/ )
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
Array.prototype.reduceRight = function( fun /*, initial*/ )
Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
Array.prototype.some = function( fun /*, thisp*/ )
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.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);
})
)
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
Tests whether all elements in the array pass the test implemented by the provided function.
Array.prototype.every = function( fun /*, thisp*/ )
Creates a new array with all elements that pass the test implemented by the provided function.
Array.prototype.filter = function( fun /*, thisp*/ )
Executes a provided function once per array element.
Array.prototype.forEach = function( fun /*, thisp*/ )
Returns the first index at which a given element can be found in the array, or -1 if it is not present.
Array.prototype.indexOf = function( elt /*, from*/ )
Returns the last index at which a given element can be found in the array, or -1 if it is not present.
Array.prototype.lastIndexOf = function( elt /*, from*/ )
Creates a new array with the results of calling a provided function on every element in this array.
Array.prototype.map = function( fun /*, thisp*/ )
returns an array of size count containing null objects
Array.dim = function( count )
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.
Array.prototype.reduceRight = function( fun /*, initial*/ )
Tests whether some element in the array passes the test implemented by the provided function.
Array.prototype.some = function( fun /*, thisp*/ )
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 )
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()