Provides utility methods for working with arrays. Additional array helpers can
be found in the collection and array-extras modules.
Y.Array(thing) returns a native array created from thing. Depending on
thing's type, one of the following will happen:
Array.test()) are converted to arrays.Note: elements that are also collections, such as <form> and <select>
elements, are not automatically converted to arrays. To force a conversion,
pass true as the value of the force parameter.
dedupearray
Dedupes an array of strings, returning an array that's guaranteed to contain only one copy of a given string.
This method differs from Array.unique() in that it's optimized for use only
with strings, whereas unique may be used with other types (but is slower).
Using dedupe() with non-string values may result in unexpected behavior.
array
String[]
Array of strings to dedupe.
eacharray
fn
[thisObj]
Executes the supplied function on each item in the array. This method wraps
the native ES5 Array.forEach() method if available.
everya
f
[o]
Executes the supplied function on each item in the array. Iteration stops if the supplied function does not return a truthy value.
filtera
f
[o]
Executes the supplied function on each item in the array. Returns a new array containing the items for which the supplied function returned a truthy value.
finda
f
[o]
Executes the supplied function on each item in the array, searching for the first item that matches the supplied function.
true for,
or null if it never returns true.
flattena
Flattens an array of nested arrays at any abitrary depth into a single, flat array.
a
Array
Array with nested arrays to flatten.
forEachAlias for each().
grepa
pattern
Iterates over an array, returning a new array of all the elements that match the supplied regular expression.
hashkeys
[values]
Returns an object using the first array as keys and the second as values. If
the second array is not provided, or if it doesn't contain the same number of
values as the first array, then true will be used in place of the missing
values.
Y.Array.hash(['a', 'b', 'c'], ['foo', 'bar']);
// => {a: 'foo', b: 'bar', c: true}
indexOfarray
value
[from=0]
Returns the index of the first item in the array that's equal (using a strict
equality check) to the specified value, or -1 if the value isn't found.
This method wraps the native ES5 Array.indexOf() method if available.
-1 if not
found.
invokeitems
name
[args*]
Executes a named method on each item in an array of objects. Items in the array that do not have a function by that name will be skipped.
Y.Array.invoke(arrayOfDrags, 'plug', Y.Plugin.DDProxy);
lastIndexOfa
val
[fromIndex]
Returns the index of the last item in the array that contains the specified
value, or -1 if the value isn't found.
a
Array
Array to search in.
val
Any
Value to search for.
[fromIndex]
Number
optional
Index at which to start searching backwards.
Defaults to the array's length - 1. If negative, it will be taken as an offset
from the end of the array. If the calculated index is less than 0, the array
will not be searched and -1 will be returned.
-1 if not
found.
mapa
f
[o]
Executes the supplied function on each item in the array and returns a new array containing all the values returned by the supplied function.
// Convert an array of numbers into an array of strings.
Y.Array.map([1, 2, 3, 4], function (item) {
return '' + item;
});
// => ['1', '2', '3', '4']
numericSorta
b
Numeric sort convenience function.
The native Array.prototype.sort() function converts values to strings and
sorts them in lexicographic order, which is unsuitable for sorting numeric
values. Provide Array.numericSort as a custom sort function when you want
to sort values in numeric order.
[42, 23, 8, 16, 4, 15].sort(Y.Array.numericSort);
// => [4, 8, 15, 16, 23, 42]
partitiona
f
[o]
Partitions an array into two new arrays, one with the items for which the
supplied function returns true, and one with the items for which the function
returns false.
matches and rejects. Each is
an array containing the items that were selected or rejected by the test
function (or an empty array if none).
reducea
init
f
[o]
Executes the supplied function on each item in the array, "folding" the array into a single value.
rejecta
f
[o]
The inverse of Array.filter(). Executes the supplied function on each item.
Returns a new array containing the items for which the supplied function
returned false.
false.
somearray
fn
[thisObj]
Executes the supplied function on each item in the array. Returning a truthy value from the function will stop the processing of remaining items.
true if the function returns a truthy value on any of the
items in the array; false otherwise.
testobj
Evaluates obj to determine if it's an array, an array-like collection, or
something else. This is useful when working with the function arguments
collection and HTMLElement collections.
Note: This implementation doesn't consider elements that are also
collections, such as <form> and <select>, to be array-like.
obj
Object
Object to test.
uniquearray
[testFn]
Returns a copy of the input array with duplicate items removed.
Note: If the input array only contains strings, the Y.Array.dedupe() method is
a much faster alternative.