Array

Additional functions on the JS Array object

Summary
ArrayAdditional functions on the JS Array object
Functions
everyTests whether all elements in the array pass the test implemented by the provided function.
filterCreates a new array with all elements that pass the test implemented by the provided function.
forEachExecutes a provided function once per array element.
indexOfReturns the first index at which a given element can be found in the array, or -1 if it is not present.
lastIndexOfReturns the last index at which a given element can be found in the array, or -1 if it is not present.
mapCreates a new array with the results of calling a provided function on every element in this array.
dimreturns an array of size count containing null objects
reduceApply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
reduceRightApply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
someTests whether some element in the array passes the test implemented by the provided function.
minreturns the “smallest” member of the array.
maxreturns the “largest” member of the array.
sumreturns a sum of the array.
avgreturns an average of the array.
lastreturns the last value in this array
firstreturns the first value in this array
parseStatic function that returns an array from an array like object, or null if conversion is not possible
compactremoves undefined values from an array
containssearches an array and returns true if one or more are found
appendUniqueadds a new element to this array, if that element is not already in this array.
getUniquereturns a copy of this array that only contains unique items
toDataSetreturns a new DataSet object based on this array

Functions

every

if (!Array.prototype.every)

Tests whether all elements in the array pass the test implemented by the provided function.

See

https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /every

filter

if (!Array.prototype.filter)

Creates a new array with all elements that pass the test implemented by the provided function.

See

https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /filter

forEach

if (!Array.prototype.forEach)

Executes a provided function once per array element.

See

https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /forEach

indexOf

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.

See

https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /indexOf

lastIndexOf

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.

See

https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /lastIndexOf

map

if (!Array.prototype.map)

Creates a new array with the results of calling a provided function on every element in this array.

See

https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /map

dim

if (!Array.dim)

returns an array of size count containing null objects

Parameters

countnumber of integers in returned array

Example

var strings = Array.dim(5).map(function(element,index){
return "String " + index++;
})

reduce

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.

See

https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /reduce

reduceRight

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.

See

https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /reduceRight

some

if (!Array.prototype.some)

Tests whether some element in the array passes the test implemented by the provided function.

See

https://developer.mozilla.org- /En- /Core_JavaScript_1.5_Reference- /Global_Objects- /Array- /some

min

Array.prototype.min = function(compare)

returns the “smallest” member of the array.

Parameters

compareOptional, default: function(a,b){return a < b} A compare function like sort() uses to determine the minimum value

max

Array.prototype.max = function(compare)

returns the “largest” member of the array.

Parameters

compareOptional, default: function(a,b){return a > b} A compare function like sort() uses to determaxe the maximum value

sum

Array.prototype.sum = function(accessor)

returns a sum of the array.

Parameters

accessorOptional, 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.

avg

Array.prototype.avg = function(accessor)

returns an average of the array.

Parameters

accessorOptional, 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;
}

last

Array.prototype.last = function()

returns the last value in this array

Parameters

defaultValueOptional, default throws error If defined, this value will be returned if the Array is empty.  Otherwise an error is thrown

first

Array.prototype.first = function()

returns the first value in this array

Parameters

defaultValueOptional, default throws error If defined, this value will be returned if the Array is empty.  Otherwise an error is thrown

parse

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

Parameters

objobject to convert
accessFunctionOptional, default function(obj,index){return obj[index]} function that takes obj and an index and returns the item at that index
lengthFunctionOptional, default function(obj){return obj.length} function that takes obj and returns the length of the collection

Detail

Takes an array-like object and returns an array containing its items.

Example

//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);
})
)

Note

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..*)

compact

Array.prototype.compact = function compactArray()

removes undefined values from an array

Detail

Modifies an array in place by removing values and renumbering the indexes

Example

//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

contains

Array.prototype.contains = function contains(search)

searches an array and returns true if one or more are found

Parameters

searcha 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

Note

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

Examples

//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);
})
})

appendUnique

Array.prototype.appendUnique=function(val,
looseCheck,
accessor)

adds a new element to this array, if that element is not already in this array.

Parameters

valvalue to append
looseCheckOptional, 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 (==)
accessorOptional, default false function to run against each item to retrieve the compared value Returns: val

getUnique

Array.prototype.getUnique=function(looseCheck,
accessor)

returns a copy of this array that only contains unique items

Parameters

looseCheckOptional, 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 (==)
accessorOptional, default false function to run against each item to retrieve the compared value

Examples

//exact matching 1 != "1"
someArray.getUnique();

//loose matching 1 == "1"
someArray.getUnique(true);

//exact object matching 1 != "1"
someArray.getUnique(false,function(e){return e.name});

//loose object matching 1 == "1"
someArray.getUnique(true,function(e){return e.name});

toDataSet

Array.prototype.toDataSet=function(columns)

returns a new DataSet object based on this array

Parameters

columnsOptional, default first row properties Array of strings.  If defined this overrides DataSet’s column detection

Note

if DataSet is not included, this array is returned

if (!Array.prototype.every)
Tests whether all elements in the array pass the test implemented by the provided function.
if (!Array.prototype.filter)
Creates a new array with all elements that pass the test implemented by the provided function.
if (!Array.prototype.forEach)
Executes a provided function once per array element.
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.
if (!Array.dim)
returns an array of size count containing null objects
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.
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.
Array.prototype.min = function(compare)
returns the “smallest” member of the array.
Array.prototype.max = function(compare)
returns the “largest” member of the array.
Array.prototype.sum = function(accessor)
returns a sum of the array.
Array.prototype.avg = function(accessor)
returns an average of the array.
Array.prototype.last = function()
returns the last value in this array
Array.prototype.first = function()
returns the first value in this array
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
Array.prototype.compact = function compactArray()
removes undefined values from an array
Array.prototype.contains = function contains(search)
searches an array and returns true if one or more are found
Array.prototype.appendUnique=function(val,
looseCheck,
accessor)
adds a new element to this array, if that element is not already in this array.
Array.prototype.getUnique=function(looseCheck,
accessor)
returns a copy of this array that only contains unique items
Array.prototype.toDataSet=function(columns)
returns a new DataSet object based on this array