Namespace: Array

XtraUtils.Array

Properties:
Name Type Description
aidsIn function

The Array class.

Properties
Name Type Description
prototype Array

the Array prototype.

Source:

Methods

(static) diff(arr1, arr2) → {Array.<Diff>}

Calculates the difference between two arrays.

Parameters:
Name Type Description
arr1 Array

The 'old' value.

arr2 Array

The 'new' value.

Source:
Returns:

An array of diffs.

Type
Array.<Diff>

(static) fromArrayLike(arrayLike) → {Array}

Returns an array that is the same as the first paramater in indices and values.

Parameters:
Name Type Description
arrayLike Object

the object to create an array from.

Source:
Returns:

The resulting array.

Type
Array

(static) intersection(arr1, arr2) → {Array}

Finds all values that are in both arrays.

Parameters:
Name Type Description
arr1 Array

The first array.

arr2 Array

The second array.

Source:
Returns:

An array of the values that are the same.

Type
Array
Example
let myFirstArray = [1, 2, 3],
	mySecondArray = [2, 3, 4];
Array.intersection(myFirstArray, mySecondArray);	// [2, 3]

(static) permutations(arr) → {Array.<Array>}

Finds all permutations, or possible combinations, of the array.

Parameters:
Name Type Description
arr Array

The array to find permutations of.

Source:
Returns:

An array of permutations.

Type
Array.<Array>

(static) range(min, max, minInclusiveopt, maxInclusiveopt) → {Array.<Number>}

Creates an array representing the range between two numbers.

Parameters:
Name Type Attributes Default Description
min number

The lower limit on the range.

max number

The upper limit on the range.

minInclusive boolean <optional>
true

Whether to include min in the range.

maxInclusive boolean <optional>
true

Whether to include the maximum in the range.

Source:
Returns:

The range.

Type
Array.<Number>
Example
Array.range(1, 4, true, true);    // [1, 2, 3, 4]

Array.range(1, 4, false, true);   // [2, 3, 4]

Array.range(1, 4, false, false);  // [2, 3]

(static) union(arr1, arr2) → {Array}

Returns all values in all arrays, minus duplicates.

Parameters:
Name Type Description
arr1 Array

The first array.

arr2 Array

The second array.

Source:
Returns:

The union of the two arrays.

Type
Array
Example
Array.union([1, 2, 3], [2, 3, 4]);	// [1, 2, 3, 4]

(static) unique(arr) → {Array}

Removes duplicate values in an array.

Parameters:
Name Type Description
arr Array

The array to remove duplicate values from.

Source:
Returns:

The uniquified array.

Type
Array
Example
Array.unique([1, 2, 3, 1, '1']);		// [1, 2, 3, '1']

(static) unzip(zippedArr) → {Array.<Array>}

Reverses Array.zip.

Parameters:
Name Type Description
zippedArr Array

The array to unzip.

Source:
Returns:

The resulting unzipped arrays.

Type
Array.<Array>

(static) xor(arr1, arr2) → {Array}

Returns all values that are in one array but not the other.

Parameters:
Name Type Description
arr1 Array

The first array.

arr2 Array

The second array.

Source:
Returns:

The logical XOR of the two arrays.

Type
Array
Example
Array.xor([1, 2, 3] [2, 3, 4]);		// [1, 4]

(static) zip(…arrays) → {Array.<Array>}

Zips the arrays so that they are parallel.

Parameters:
Name Type Attributes Description
arrays Array <repeatable>

The arrays to zip.

Source:
Returns:

The resulting zipped arrays.

Type
Array.<Array>

chunks(chunkWidth) → {Array.<Array>}

Gets the chunks of an array, where chunks are sections of the array with a constant width.

Parameters:
Name Type Description
chunkWidth number

The width of the chunks.

Source:
Returns:

The chunks of the array.

Type
Array.<Array>

compact(compactoropt, originalFlagopt) → {Array}

Filters out values. Similar to Array.prototype.filter.

Parameters:
Name Type Attributes Default Description
compactor ArrayCallback <optional>
(_)=>{return (typeof _ === 'number') ? true : Boolean(_)}

The function to compact it with.

originalFlag boolean <optional>
false

Whether to compact the original array.

Source:
Returns:

The compacted array.

Type
Array

concatMap(fn) → {Array}

Maps the array and concatenates the results.

Parameters:
Name Type Description
fn ArrayCallback

The mapper.

Source:
Returns:

The mapped and concatenated result.

Type
Array

deepFlatMap(fnopt, originalFlagopt) → {Array}

Maps the array, then flattens it to a depth of 0.

Parameters:
Name Type Attributes Default Description
fn ArrayCallback <optional>
(..._)=>{return _[0]}

The mapping function.

originalFlag boolean <optional>
false

Whether to give the function the original array.

Source:
Returns:

The flattend and mapped array.

Type
Array

deepFlatten(originalFlagopt) → {Array}

Flattens the object recursively, so that it has a depth of 0.

Parameters:
Name Type Attributes Default Description
originalFlag boolean <optional>
false

Whether to modify the original array.

Source:
Returns:

The smallest number in the array.

Type
Array

first(nopt) → {Array}

Gets the first n elements in the array.

Parameters:
Name Type Attributes Default Description
n number <optional>
1

The number of elements to get off of the beginnng of the array.

Source:
Returns:

The first n elements in the array.

Type
Array
Example
[1, 2, 3].first(2);		// [1, 2]

flatten(depthopt, originalFlagopt) → {Array}

Flattens the array by the specified amount.

Parameters:
Name Type Attributes Default Description
depth number <optional>
1

The amount to flatten it by.

originalFlag boolean <optional>
false

Whether to modify the original array.

Source:
Returns:

The flattened array.

Type
Array

initial(nopt) → {Array}

Gets all the elements of an array except the last n elements.

Parameters:
Name Type Attributes Default Description
n number <optional>
1

The number of elements to exclude.

Source:
Returns:

The elements in the array except the last n elements.

Type
Array
Example
[1, 2, 3].initial(2);	// [1]

last(nopt) → {Array}

Gets the last n elements in the array.

Parameters:
Name Type Attributes Default Description
n number <optional>
1

The number of elements to get off of the end of the array.

Source:
Returns:

The last n elements in the array.

Type
Array
Example
[1, 2, 3].last(2);	// [2, 3]

max() → {number}

Finds the maximum of the array.

Source:
Returns:

The largest number in the array.

Type
number
Example
[1, 2, 3].max();	// 3
[-1, -4, -2].max();	// -1

mean() → {number}

Gets the mean or average of the array.

Source:
Throws:

All items of the array must be numbers, or valid number strings.

Type
TypeError
Returns:

The average.

Type
number

min() → {number}

Finds the smallest number in the array.

Source:
Returns:

The smallest number in the array.

Type
number

one(fn, originalFlagopt, …argsopt) → {boolean}

Returns true if only one item in the array passes the test.

Parameters:
Name Type Attributes Default Description
fn ArrayCallBack

The function.

originalFlag boolean <optional>
false

Whether to give the function the original array.

args * <optional>
<repeatable>

Additional arguments to give the function.

Source:
Returns:

Whether only one item in the array passes the test.

Type
boolean
Example
[1, 2, 3].one((item)=>{return item === 1});		// true

padLeft(length, objectopt) → {Array}

Pad an array to the left. Does not modify the original array.

Parameters:
Name Type Attributes Default Description
length number

The length to pad to.

object * <optional>
0

The element to pad with.

Source:
Returns:

The resulting padded array.

Type
Array

padRight(length, objectopt) → {Array}

Pads the array, adding to the right.

Parameters:
Name Type Attributes Default Description
length number

The length to pad to.

object * <optional>
0

The element to pad with.

Source:
Returns:

The padded array.

Type
Array

random() → {*}

Gets a random item from the array.

Source:
Returns:

The item.

Type
*

repeat(times) → {Array}

Makes the array repeat times times.

Parameters:
Name Type Description
times number

How many times to repeat the array. Can be a decimal, if so, rounds to the nearest whole number length.

Source:
Returns:

The repeated array.

Type
Array

sample(n) → {Array.<*>}

Gets n random items from the array.

Parameters:
Name Type Description
n number

How many items.

Source:
Returns:

The items.

Type
Array.<*>

shallowClone() → {Array}

Shallow clones the object, returning an array with the same length, and the same properties, but not equal.

Source:
Returns:

The cloned array.

Type
Array

shuffle(originalFlagopt) → {Array}

Shuffles the elements in the array. By default does not modify the original array.

Parameters:
Name Type Attributes Default Description
originalFlag boolean <optional>
false

Whether to modify the original array.

Source:
Returns:

The shuffled array.

Type
Array

Type Definitions

ArrayCallback(item, index, array) → {*}

Parameters:
Name Type Description
item *

The item.

index number

The index of the item in the array.

array Array

The array being mapped.

Source:
Returns:

The result.

Type
*

Diff

The diff object returned by Array.diff.

Type:
  • Object
Properties:
Name Type Description
before *

The value before changes applied.

after *

The value after changes were applied.

index number

The index in the array.

Source: