ECMAScript | |
---|---|
Array | concat ( Object element1, … ) |
Boolean | every ( Function callbackfn, Object thisArg=undefined ) |
Array | filter ( Function callbackfn, Object thisArg=undefined ) |
void | forEach ( Function callbackfn, Object thisArg=undefined ) |
Number | indexOf ( Object searchElement, Number fromIndex=0 ) |
String | join ( String separator=“,” ) |
Number | lastIndexOf ( Object searchElement, Number fromIndex=length-1 ) |
Array | map ( Function callbackfn, Object thisArg=undefined ) |
Object | pop () |
Number | push ( Object element1, … ) |
Object | reduce ( Function callbackfn, Object initialValue=undefined ) |
Object | reduceRight ( Function callbackfn, Object initialValue=undefined ) |
Array | reverse () |
Object | shift () |
Array | slice ( Number startIndex, Number endIndex=length ) |
Boolean | some ( Function callbackfn, Object thisArg=undefined ) |
Array | sort ( Function comparisonFunction ) |
Array | splice ( Number startIndex, Number deleteCount, … ) |
String | toLocaleString () |
String | toString () |
Number | unshift ( Object element1, … ) |
DAZ Script | |
Number | find ( Object element ) |
void | pushIfNotExists ( Object element1, … ) |
An array is a data type that allows you to work with a list of elements. These elements can be any Object or QObject derived object. Multi-dimensional arrays are achieved by using array elements that are arrays themselves.
Array Creation
Arrays can be constructed using array literals, or by using the new operator. They can be extended dynamically by simply creating elements at non-existent index positions.
Example:
Array literal
var aTemp = [];
Example:
Array literal
var aTemp = [ "a", "b", "c", "d" ];
Example:
Using the new operator, initialized with a size, but with undefined elements
var aTemp = new Array( 4 );
Example:
Using the new operator, defining all elements
var aTemp = new Array( "a", "b", "c", "d" );
Example:
Multi-Dimensional, dynamically extended
var aTemp = [ "a", "b", "c", "d" ]; var aTemp2 = []; for ( var i = 0; i < aTemp.length; i++ ) { aTemp2[ i ] = new Array( 2 ); aTemp2[ i ][ 0 ] = i; aTemp2[ i ][ 1 ] = aTemp[ i ]; } // aTemp2 == [ [ 0, "a" ], [ 1, "b" ], [ 2, "c" ], [ 3, "d" ] ]
Array Access
Array elements are accessed by their names. Names can be integers or strings. As integers, element names are zero-based, meaning the first element is at 0, the second element is at 1 and so on. As strings, elements can act as normal properties, and can be accessed by using the square bracket operator ([]) or by directly dereferencing the Array object and specifying the property name (.name). These two accessor types can be mixed freely.
Example:
Integer access
var aTemp = [ "a", "b", "c", "d" ]; var sTemp = aTemp[ 2 ]; // sTemp == "c";
Example:
Multi-Dimensional, integer access
var aTemp = [ [ "a", 5 ], [ "b", 10 ], [ "c", 15 ], [ "d", 20 ] ]; var nTemp = aTemp[ 2 ][ 1 ]; // nTemp == 15;
Example:
String access, using the square bracket operator
var aTemp = []; // Assignment: aTemp[ "one" ] = "a"; aTemp[ "two" ] = "b"; // Retrieval: var sTemp = aTemp[ "one" ]; // sTemp == "a" var sTemp2 = aTemp[ "two" ]; // sTemp2 == "b"
Example:
String access, dereferencing the Array, specifying properties
var aTemp = []; // Assignment: aTemp.one = "a"; aTemp.two = "b"; // Retrieval: var sTemp = aTemp.one; // sTemp == "a" var sTemp2 = aTemp.two; // sTemp2 == "b"
Array Iteration
An array can be iterated over by using a for or for...in loop.
Example:
Iteration of element names as integers, using the for control statement
var aTemp = [ "a", "b", "c", "d" ]; var sTemp = ""; for( var i = 0; i < aTemp.length; i++ ){ sTemp += aTemp[ i ]; } // sTemp == "abcd"
Example:
Iteration of element names as strings, using the for...in control statement.
var aTemp = []; aTemp[ "first" ] = "a"; aTemp[ "second" ] = "b"; aTemp[ "third" ] = "c"; aTemp[ "fourth" ] = "d"; // element names are sorted alphanumerically // aTemp == [ "first" : "a", "fourth" : "d", "second" : "b", "third" : "c" ] var sTemp = ""; for( var sElement in aTemp ){ sTemp += aTemp[ sElement ]; } // sTemp == "adbc"
Todo
Investigate whether there is a (Qt) bug in the return value of reverse, sort and slice; reference vs. copy.
Holds the number of elements in the array. (Read Only)
Example:
var aTemp = [ "a", "b", "c", "d" ]; print( aTemp.length ); //4
Boolean : isArray( Object obj )
Return Value:
true
if obj
is an array, otherwise false
.Example:
var aTemp = []; print( Array.isArray( aTemp ) ); //true var oTmp = {}; print( Array.isArray( oTmp ) ); //false
Array()
Default Constructor.
Example:
var aTemp = new Array(); print( aTemp.length ); //0
Array( Object firstElement, … )
Constructs an array with one or more elements.
Parameter(s):
Example:
var aTemp = new Array( "a", "b", "c", "d" ); print( aTemp.length ); //4
Constructs an array of the given length. The arrays elements will be uninitialized (undefined).
Parameter(s):
Example:
var aTemp = new Array( 10 ); print( aTemp.length ); //10
Array : concat( Object element1, … )
Parameter(s):
Return Value:
Example:
var aTemp = [ "a", "b", "c" ]; print( aTemp ); //a,b,c var nTemp = 5; print( aTemp.concat( nTemp ) ); //a,b,c,5
var aTemp = [ "a", "b", "c" ]; print( aTemp ); //a,b,c var aTemp2 = [ 1, 2, 3 ]; print( aTemp2 ); //1,2,3 print( aTemp.concat( aTemp2 ) ); //a,b,c,1,2,3
Boolean : every( Function callbackfn, Object thisArg=undefined )
Invokes a function for each non-empty element in the array, in ascending order, until or unless that function returns false
.
Parameter(s):
true
or false
; the arguments are the value of the element, the index of the element and the object being traversedcallbackfn
.Return Value:
true
if callbackfn
returns true
for every element in the array, otherwise false
.Example:
function greaterThan5( nValue, nIndex, aValues ) { return nValue > 5; } function greaterThan10( nValue, nIndex, aValues ) { return nValue > 10; } var aTmp = [10, 20, 30, 40, 50]; print( aTmp.every( greaterThan5 ) ); //true print( aTmp.every( greaterThan10 ) ); //false
Array : filter( Function callbackfn, Object thisArg=undefined )
Invokes a function for each non-empty element in the array.
Parameter(s):
true
or false
; the arguments are the value of the element, the index of the element and the object being traversed.callbackfn
.Return Value:
callbackfn
returns true
.Example:
function evenNumber( nValue, nIndex, aValues ) { var nRemainder = nValue % 2; return nRemainder == 0; } function oddNumber( nValue, nIndex, aValues ) { var nRemainder = nValue % 2; return nRemainder == 1; } var aTemp = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; print( aTemp.filter( evenNumber ) ); //2,4,6,8,10 print( aTemp.filter( oddNumber ) ); //1,3,5,7,9
void : forEach( Function callbackfn, Object thisArg=undefined )
Invokes a function for each non-empty element in the array.
Parameter(s):
callbackfn
.Example:
function printValue( nValue, nIndex, aValues ) { print( nIndex, nValue, aValues[ nIndex ] ); } var aTemp = [10, 20, 30, 40, 50]; aTemp.forEach( printValue ); //0 10 10 //1 20 20 //2 30 30 //3 40 40 //4 50 50
Number : indexOf( Object searchElement, Number fromIndex=0 )
Compares searchElement
to the elements of the array, in ascending order, using Strict Equals.
Parameter(s):
Return Value:
searchElement
in the array, or -1
if searchElement
is not found.Example:
var aTemp = [ "a", "b", "c", "d" ]; print( aTemp.indexOf( "c" ) ); //2
String : join( String separator=“,” )
Parameter(s):
Return Value:
separator
between each.Example:
var aTemp = [ "a", "b", "c", "d" ]; print( aTemp.join( ":" ) ); //a:b:c:d
See Also:
Number : lastIndexOf( Object searchElement, Number fromIndex=length-1 )
Compares searchElement
to the elements of the array, in descending order, using strict equality.
Parameter(s):
Return Value:
searchElement
in the array, or -1
if searchElement
is not found.Example:
var aTemp = [ "a", "b", "c", "d", "a", "b", "c", "d" ]; print( aTemp.lastIndexOf( "c" ) ); //6
Array : map( Function callbackfn, Object thisArg=undefined )
Invokes a function for each non-empty element in the array.
Parameter(s):
callbackfn
.Return Value:
callbackfn
for each non-empty element in the array.Example:
function addDoe( sValue, nIndex, aValues ) { return sValue + " Doe"; } var aTemp = ["John", "Jane", "Baby"]; print( aTemp.map( addDoe ) ); //John Doe,Jane Doe,Baby Doe
Removes the last (right-most) element from the array.
Return Value:
Example:
var aTemp = [ "a", "b", "c", "d" ]; print( aTemp ); //a,b,c,d print( aTemp.pop() ); //d print( aTemp ); //a,b,c
Number : push( Object element1, … )
Appends one or more elements onto the end (right) of the array.
Parameter(s):
Return Value:
Example:
var aTemp = [ "a", "b", "c", "d" ]; print( aTemp ); //a,b,c,d print( aTemp.push( "e" ) ); //a,b,c,d,e
Object : reduce( Function callbackfn, Object initialValue=undefined )
Invokes a function for each element in the array, in ascending order.
Parameter(s):
callbackfn
; if not defined previousValue will be the first element in the array and currentValue will be the second element.Return Value:
callbackfn
for each element in the array.Example:
function accumulate( nPreviousValue, nValue, nIndex, aValues ) { return nPreviousValue + nValue; } var aTemp = [10, 20, 30, 40, 50]; print( aTemp.reduce( accumulate, 5 ) ); //155
Object : reduceRight( Function callbackfn, Object initialValue=undefined )
Invokes a function for each element in the array, in descending order.
Parameter(s):
callbackfn
; if not defined previousValue will be the first element in the array and currentValue will be the second element.Return Value:
callbackfn
for each element in the array.Example:
function accumulate( nPreviousValue, nValue, nIndex, aValues ) { return nPreviousValue - nValue; } var aTemp = [10, 20, 30, 40, 50]; print( aTemp.reduceRight( accumulate, 155 ) ); //5
Array : reverse()
Reverses the order of the elements in the array.
Return Value:
Example:
var aTemp = [ "a", "b", "c", "d" ]; print( aTemp.reverse() ); //d,c,b,a
var aTemp = [ "a", "b", "c", "d" ]; print( aTemp ); //a,b,c,d print( aTemp.reverse() ); //d,c,b,a
Removes the first (left-most) element in the array.
Return Value:
Example:
var aTemp = [ "a", "b", "c", "d" ]; print( aTemp ); //a,b,c,d var sTemp = aTemp.shift(); print( sTemp ); //a print( aTemp ); //b,c,d
Array : slice( Number startIndex, Number endIndex=length )
Parameter(s):
Return Value:
startIndex
to (but not including) the element at endIndex
.Example:
var aTemp = [ "a", "b", "c", "d", "e", "f" ]; var aTemp2 = aTemp.slice( 2 ); print( aTemp ); //a,b,c,d,e,f print( aTemp2 ); //c,d,e,f
var aTemp = [ "a", "b", "c", "d", "e", "f" ]; var aTemp2 = aTemp.slice( 1, 4 ); print( aTemp ); //a,b,c,d,e,f print( aTemp2 ); //b,c,d
Boolean : some( Function callbackfn, Object thisArg=undefined )
Invokes a function for each non-empty element in the array, in ascending order.
Parameter(s):
true
or false
; the arguments are the value of the element, the index of the element and the object being traversed.callbackfn
.Return Value:
true
if callbackfn
returns true
for any element in the array, otherwise false
.Array : sort( Function comparisonFunction )
Sorts the array using comparisonFunction
. If no Function is provided, default sorting is applied.
Parameter(s):
Return Value:
Example:
No arguments
var aTemp = new [ "a", "c", "d", "b" ]; aTemp.sort(); // aTemp == [ "a", "b", "c", "d" ]
Example:
Comparison function
// -1 if a < b, 0 if a == b, 1 if a > b function numerically( a, b ){ return a < b ? -1 : a > b ? 1 : 0; } var aTemp = [ 8, 90, 1, 4, 843, 221 ]; aTemp.sort( numerically ); // aTemp == [ 1, 4, 8, 90, 221, 843 ]
Array : splice( Number startIndex, Number deleteCount, … )
Splices elements into the array.
Parameter(s):
Return Value:
Example:
Insert
var aTemp = [ "a", "b", "c", "d" ]; aTemp.splice( 2, 0, "X" ); print( aTemp ); //a,b,X,c,d
var aTemp = [ "a", "b", "c", "d" ]; aTemp.splice( 2, 0, "X", "Y" ); print( aTemp ); //a,b,X,Y,c,d
Example:
Remove
var aTemp = [ "a", "b", "c", "d" ]; var aTemp2 = aTemp.splice( 2, 1 ); print( aTemp ); //a,b,d print( aTemp2 ); //c
Example:
Replace
var aTemp = [ "a", "b", "c", "d" ]; var aTemp2 = aTemp.splice( 2, 1, "X" ); print( aTemp ); //a,b,X,d print( aTemp2 ); //c
var aTemp = [ "a", "b", "c", "d" ]; var aTemp2 = aTemp.splice( 2, 1, "X", "Y" ); print( aTemp ); //a,b,X,Y,d print( aTemp2 ); //c
String : toLocaleString()
Provides a locale-aware version of toString().
Return Value:
See Also:
Return Value:
Example:
var aTemp = [ "a", "b", "c", "d" ]; var sTemp = aTemp.toString(); // sTemp == "a,b,c,d"
See Also:
Number : unshift( Object element1, … )
Inserts the element(s) at the beginning (left) of the array.
Parameter(s):
Return Value:
Example:
var aTemp = [ "a", "b", "c", "d" ]; var nTemp = aTemp.unshift( "X" ); print( aTemp ); //X,a,b,c,d print( nTemp ); //5
var aTemp = [ "a", "b", "c", "d" ]; var nTemp = aTemp.unshift( "X", "Y" ); print( aTemp ); //X,Y,a,b,c,d print( nTemp ); //6
Number : find( Object element )
Parameter(s):
Return Value:
element
, if found. -1
if element
is not found.Example:
var aTemp = [ "a", "b", "c", "d", "e" ]; print( aTemp.find( "d" ) ); //3
void : pushIfNotExists( Object element1, … )
Pushes one or more elements onto the end (right) of the array, if it does not already exist in the array.
Parameter(s):
Example:
var aTemp = [ "a", "b", "c", "d" ]; aTemp.pushIfNotExists( "e" ); // aTemp == [ "a", "b", "c", "d", "e" ]