The methods in the UsefulJS.Array
namespace are not designed to be called by application
code. Rather, they are the implementations of the ECMAScript 5/6 Array methods and intended to be
called via methods of Array.prototype
. The methods are generic in the sense that
they will work with any array-like object.
Fills slots in an array between specified boundaries with a specified value. This function modifies its input but won't grow it.
UsefulJS.Array.fill(a, value[, start[, end]])
a
Arrayvalue
Objectstart
Numberend
NumberReturns: Array the mutated array
Finds an item in an Array starting either from the head or the tail.
UsefulJS.Array.find(a, item, idx, incr)
a
Arrayitem
Objectidx
Numberincr
NumberReturns: Number either the 0-based index of the item or -1
Iterates over an array calling a function for each item in the array until either the array is exhausted or (in some modes) the callback returns a certain value.
UsefulJS.Array.iterate(a, callback, ctx, mode)
a
Arraycallback
Functionf(item, index, array)
ctx
Objectthis
context for the callbackmode
StringReturns: varying, according to the mode:
true
or undefinedtrue
or -1Iterates over an array passing in an accumulated result and the current item in the array; the final output is a single value.
UsefulJS.Array.reduce(a, callback, initialValue, incr)
a
Arraycallback
Functionf(accumulated, item, index, array)
initialValue
Objectincr
NumberReturns: Object the final return value from the callback.
The fixes for the UsefulJS.Array module are defined in the _array namespace of the input and
output properties of the fix
call. All are applied by default. The property names
are the method names: "indexOf", "lastIndexOf", "from", etc.
Array.isArray(obj)
See the the MDN documentation for details
array.copyWithin(targetIdx, startIdx[, endIdx])
See the the MDN documentation for details
array.every(callback[, context])
See the the MDN documentation for details
array.fill(value[, start[, end]])
See the the MDN documentation for details
array.filter(callback[, context])
See the the MDN documentation for details
array.find(callback[, context])
See the the MDN documentation for details
array.findIndex(callback[, context])
See the the MDN documentation for details
array.forEach(callback[, context])
See the the MDN documentation for details
array.indexOf(item[, fromIndex])
See the the MDN documentation for details
array.lastIndexOf(item[, fromIndex])
See the the MDN documentation for details
array.map(callback[, context])
See the the MDN documentation for details
array.reduce(callback[, initialValue])
See the the MDN documentation for details
array.reduceRight(callback[, initialValue])
See the the MDN documentation for details
array.some(callback[, context])
See the the MDN documentation for details
Factory methods create Array objects from non-array objects. They are implemented as generic functions so that they can create instances of other classes if you copy the methods to those classes. For example:
var MyArray = function(length) { ... }; MyArray.of = Array.of; var arr = MyArray.of(1, 2, 3); // creates an instance of MyArray
Constructs an Array from an array-like object.
Array.from(obj[, mapFn[, mapCtx]])
obj
ObjectmapFn
FunctionmapCtx
Objectthis
in the mapping function.Returns: Array
Array-like objects have a length property and can be accessed via square-bracket indexing but do not
otherwise have the methods of a real Array. Examples of array-like objects are strings (assuming square
bracket indexing is supported), NodeList
objects and Element.classList
.
Array.from will return a real Array that contains the same values as its input. See the MDN documentation for more information.
Constructs an Array from its arguments.
Array.of([item1 [, item2, ... ]])
Returns: Array
Arguments can be any number of any objects. See the MDN documentation for more information.