Class YUI~array~extras
Adds the following array utilities to the YUI instance
(Y.Array). This is in addition to the methods provided
in the core.
Properties
Returns the index of the last item in the array
that contains the specified value, -1 if the
value isn't found.
method Array.lastIndexOf
Methods
static
boolean
Array.every
(
a
,
f
,
o
)
Executes the supplied function on each item in the array.
- Parameters:
-
a
<Array>
the array to iterate
-
f
<Function>
the function to execute on each item
-
o
<object>
Optional context object
- Returns:
boolean
- true if every item in the array returns true
from the supplied function.
static
Array
Array.filter
(
a
,
f
,
o
)
Executes the supplied function on each item in the array.
Returns a new array containing the items that the supplied
function returned true for.
- Parameters:
-
a
<Array>
the array to iterate
-
f
<Function>
the function to execute on each item
-
o
<object>
Optional context object
- Returns:
Array
- The items on which the supplied function
returned true. If no items matched an empty array is
returned.
static
object
Array.find
(
a
,
f
,
o
)
Executes the supplied function on each item in the array,
searching for the first item that matches the supplied
function.
- Parameters:
-
a
<Array>
the array to search
-
f
<Function>
the function to execute on each item.
Iteration is stopped as soon as this function returns true
on an item.
-
o
<object>
Optional context object
- Returns:
object
- the first item that the supplied function
returns true for, or null if it never returns true
static
Array
Array.grep
(
a
,
pattern
)
Iterates over an array, returning a new array of all the elements
that match the supplied regular expression
- Parameters:
-
a
<Array>
a collection to iterate over
-
pattern
<RegExp>
The regular expression to test against
each item
- Returns:
Array
- All the items in the collection that
produce a match against the supplied regular expression.
If no items match, an empty array is returned.
static
Array
Array.map
(
a
,
f
,
o
)
Executes the supplied function on each item in the array.
- Parameters:
-
a
<Array>
the array to iterate
-
f
<Function>
the function to execute on each item
-
o
<object>
Optional context object
- Returns:
Array
- A new array containing the return value
of the supplied function for each item in the original
array.
static
Array.partition
(
a
,
o
)
Partitions an array into two new arrays, one with the items
that match the supplied function, and one with the items that
do not.
- Parameters:
-
a
<Array>
a collection to iterate over
-
o
<object>
Optional execution context of f.
- Returns:
- An object with two members, 'matches' and 'rejects',
that are arrays containing the items that were selected or
rejected by the test function (or an empty array).
static
Array.reduce
(
a
,
init
,
f
,
o
)
Executes the supplied function on each item in the array.
Reduce "folds" the array into a single value.
- Parameters:
-
a
<Array>
the array to iterate
-
init
<object>
The initial value to start from
-
f
<Function>
the function to execute on each item. It
is responsible for returning the updated value of the
computation.
-
o
<object>
Optional context object
- Returns:
- A value that results from iteratively applying the
supplied function to each element in the array.
static
Array
Array.reject
(
a
,
f
,
o
)
The inverse of filter. Executes the supplied function on each item.
Returns a new array containing the items that the supplied
function returned *false* for.
- Parameters:
-
a
<Array>
the array to iterate
-
f
<Function>
the function to execute on each item
-
o
<object>
Optional context object
- Returns:
Array
- The items on which the supplied function
returned false.
static
Array
Array.unique
(
a
,
sort
)
Returns a copy of the array with the duplicate entries removed
- Parameters:
-
a
<Array>
the array to find the subset of uniques for
-
sort
<bool>
flag to denote if the array is sorted or not. Defaults to false, the more general operation
- Returns:
Array
- a copy of the array with duplicate entries removed
static
Array.zip
(
a
,
a2
)
Creates an array of arrays by pairing the corresponding
elements of two arrays together into a new array.
- Parameters:
-
a
<Array>
a collection to iterate over
-
a2
<Array>
another collection whose members will be
paired with members of the first parameter
- Returns:
- An array of arrays formed by pairing each element
of the first collection with an item in the second collection
having the corresponding index.