Interface Array

Index

Methods

Methods

public add(el: T in Array<T>, index?: number): Array<T>

Adds to the array.

extra

If [index] is specified, it will add at [index], otherwise adds to the end of the array. %add% behaves like %concat% in that if is an array it will be joined, not inserted. This method will change the array! Use %include% for a non-destructive alias. Also, %insert% is provided as an alias that reads better when using an index.

example

[1,2,3,4].add(5) -> [1,2,3,4,5] [1,2,3,4].add([5,6,7]) -> [1,2,3,4,5,6,7] [1,2,3,4].insert(8, 1) -> [1,8,2,3,4]

Parameters

  • el: T in Array<T>

    Elements to add to the array.

  • index?: number optional

    Specifies the index where to insert/add into the array, default = Array.length

Returns

Array<T>

Array containing the added elements at position .

public add(el: Array<T>, index?: number): Array<T>

see

add

Parameters

  • el: Array<T>
  • index?: number optional

Returns

Array<T>

public all(f: T in Array<T>, scope?: any): boolean

see

every

Parameters

  • f: T in Array<T>
  • scope?: any optional

Returns

boolean

public all(f: (element: T, index: number, array: T[]) => boolean, scope?: any): boolean

see

every

Parameters

  • f: (element: T, index: number, array: T[]) => boolean
  • scope?: any optional

Returns

boolean

public at(index: number, loop?: boolean): T in Array<T>

Gets the element(s) at a given index.

extra

When [loop] is true, overshooting the end of the array (or the beginning) will begin counting from the other end. As an alternate syntax, passing multiple indexes will get the elements at those indexes.

example

[1,2,3].at(0) -> 1 [1,2,3].at(2) -> 3 [1,2,3].at(4) -> 2 [1,2,3].at(4, false) -> null [1,2,3].at(-1) -> 3 [1,2,3].at(0,1) -> [1,2]

Parameters

  • index: number

    Element's index to retrieve.

  • loop?: boolean optional

    Continue counting if the index overshoots the Array.length

Returns

T in Array<T>

The element at .

public at(start: number, stop: number): Array<T>

see

at

Parameters

  • start: number

    Start index.

  • stop: number

Returns

Array<T>

Elements between and

public average(map?: (e: T) => number): number

Averages all values in the array.

extra

[map] may be a function mapping the value to be averaged or a string acting as a shortcut.

example

[1,2,3].average() -> 2 [{age:35},{age:11},{age:11}].average(function(n) { return n.age; }); -> 19 [{age:35},{age:11},{age:11}].average('age') -> 19

Parameters

  • map?: (e: T) => number optional

    Maps each element to a number.

Returns

number

The average of the entire array.

public clone(): Array<T>

Clones the array.

example

[1,2,3].clone() -> [1,2,3]

Returns

Array<T>

Cloned array.

public compact(all?: boolean): Array<T>

Removes all instances of %undefined%, %null%, and %NaN% from the array.

extra

If [all] is %true%, all "falsy" elements will be removed. This includes empty strings, 0, and false.

example

[1,null,2,undefined,3].compact() -> [1,2,3] [1,'',2,false,3].compact() -> [1,'',2,false,3] [1,'',2,false,3].compact(true) -> [1,2,3]

Parameters

  • all?: boolean optional

    Remove false elements, default = false.

Returns

Array<T>

The same array with the special values removed.

public count(f: T in Array<T>): number

Counts all elements in the array that match .

extra

will match a string, number, array, object, or alternately test against a function or regex. This method implements @array_matching.

example

[1,2,3,1].count(1) -> 2 ['a','b','c'].count(/b/) -> 1 [{a:1},{b:2}].count(function(n) { return n['a'] > 1; }); -> 0

Parameters

  • f: T in Array<T>

    object to match against in the array.

Returns

number

Number of elements in the array that match .

public each(fn: (element: T, index?: number, array?: T[]) => any, index?: number, loop?: boolean): Array<T>

Runs against each element in the array. Enhanced version of %Array#forEach%.

extra

Parameters passed to are identical to %forEach%, ie. the first parameter is the current element, second parameter is the current index, and third parameter is the array itself. If returns %false% at any time it will break out of the loop. Once %each% finishes, it will return the array. If [index] is passed, will begin at that index and work its way to the end. If [loop] is true, it will then start over from the beginning of the array and continue until it reaches [index] - 1.

example

[1,2,3,4].each(function(n) { // Called 4 times: 1, 2, 3, 4 }); [1,2,3,4].each(function(n) { // Called 4 times: 3, 4, 1, 2 }, 2, true);

Parameters

  • fn: (element: T, index?: number, array?: T[]) => any

    Callback function for applied to each element in the array.

  • index?: number optional

    Starting index, default = 0.

  • loop?: boolean optional

    Continue from the beginning if the end of the array is reached, default = false.

Returns

Array<T>

Original array.

public every(f: T in Array<T>, scope?: any): boolean

Returns true if all elements in the array match .

extra

[scope] is the %this% object. %all% is provided an alias. In addition to providing this method for browsers that don't support it natively, this method also implements @array_matching.

example

['a','a','a'].every(function(n) { return n == 'a'; }); ['a','a','a'].every('a') -> true [{a:2},{a:2}].every({a:2}) -> true

Parameters

  • f: T in Array<T>

    Match all elements to this.

  • scope?: any optional

    object.

Returns

boolean

Boolean

public exclude(f?: Array<T>): Array<T>

Removes any element in the array that matches [f1], [f2], etc.

extra

This is a non-destructive alias for %remove%. It will not change the original array. This method implements @array_matching.

example

[1,2,3].exclude(3) -> [1,2] ['a','b','c'].exclude(/b/) -> ['a','c'] [{a:1},{b:2}].exclude(function(n) { return n['a'] == 1; }); -> [{b:2}]

Parameters

  • f?: Array<T> optional

    Elements to find in the array and remove.

Returns

Array<T>

A copy of the original array with all instances of removed.

public exclude(f: (element: T, index: number, array: T[]) => boolean): Array<T>

see

exclude

Parameters

  • f: (element: T, index: number, array: T[]) => boolean

Returns

Array<T>

public filter(f: T in Array<T>, scope?: any): Array<T>

Returns any elements in the array that match .

extra

[scope] is the %this% object. In addition to providing this method for browsers that don't support it natively, this method also implements @array_matching.

example

[1,2,3].filter(function(n) { return n > 1; }); [1,2,2,4].filter(2) -> 2

Parameters

  • f: T in Array<T>

    Find these elements in the array.

  • scope?: any optional

    %this% object while filtering.

Returns

Array<T>

Array containing th items found in the array.

public find(f: T in Array<T>, index?: number, loop?: boolean): T in Array<T>

Returns the first element that matches .

extra

will match a string, number, array, object, or alternately test against a function or regex. Starts at [index], and will continue once from index = 0 if [loop] is true. This method implements @array_matching.

example

[{a:1,b:2},{a:1,b:3},{a:1,b:4}].find(function(n) { return n['a'] == 1; }); -> {a:1,b:3} ['cuba','japan','canada'].find(/^c/, 2) -> 'canada'

Parameters

  • f: T in Array<T>

    Elements to match against.

  • index?: number optional

    Index to start searching from, default = 0.

  • loop?: boolean optional

    Loop around the end of the array, default = false.

Returns

T in Array<T>

First element matching .

public find(f: (element: T, index: number, array: T[]) => boolean, index?: number, loop?: boolean): T in Array<T>

see

find

Parameters

  • f: (element: T, index: number, array: T[]) => boolean
  • index?: number optional
  • loop?: boolean optional

Returns

T in Array<T>

public findAll(f: T in Array<T>, index?: number, loop?: boolean): Array<T>

Returns all elements that match .

extra

will match a string, number, array, object, or alternately test against a function or regex. Starts at [index], and will continue once from index = 0 if [loop] is true. This method implements @array_matching.

example

[{a:1,b:2},{a:1,b:3},{a:2,b:4}].findAll(function(n) { return n['a'] == 1; }); -> [{a:1,b:3},{a:1,b:4}] ['cuba','japan','canada'].findAll(/^c/) -> 'cuba','canada' ['cuba','japan','canada'].findAll(/^c/, 2) -> 'canada'

Parameters

  • f: T in Array<T>

    Element to match against.

  • index?: number optional

    Index to start searching from, default = 0.

  • loop?: boolean optional

    Loop around the end of the array, default = false.

Returns

Array<T>

Elements matching .

public findAll(f: (element: T, index: number, array: T[]) => boolean, index?: number, loop?: boolean): Array<T>

see

findAll

Parameters

  • f: (element: T, index: number, array: T[]) => boolean
  • index?: number optional
  • loop?: boolean optional

Returns

Array<T>

public findIndex(f: T in Array<T>, startIndex?: number, loop?: boolean): number

Returns the index of the first element that matches or -1 if not found.

method

findIndex(, [startIndex] = 0, [loop] = false)

extra

This method has a few notable differences to native %indexOf%. Although will similarly match a primitive such as a string or number, it will also match deep objects and arrays that are not equal by reference (%===%). Additionally, if a function is passed it will be run as a matching function (similar to the behavior of %Array#filter%) rather than attempting to find that function itself by reference in the array. Starts at [index], and will continue once from index = 0 if [loop] is true. This method implements @array_matching.

example

[1,2,3,4].findIndex(3); -> 2 [1,2,3,4].findIndex(function(n) { return n % 2 == 0; }); -> 1 ['one','two','three'].findIndex(/th/); -> 2

Parameters

  • f: T in Array<T>

    Element to match against.

  • startIndex?: number optional

    Index to start searching from, default = 0.

  • loop?: boolean optional

    Loop around the end of th array, default = false.

Returns

number

Index at which is found.

public findIndex(f: (element: T, index: number, array: T[]) => boolean, startIndex?: number, loop?: boolean): number

see

findIndex

Parameters

  • f: (element: T, index: number, array: T[]) => boolean
  • startIndex?: number optional
  • loop?: boolean optional

Returns

number

public first(): T in Array<T>

Returns the first element(s) in the array.

extra

When is passed, returns the first elements in the array.

note

If is omitted then the return type is the element itself, not an array of the element, For example 1 instead of [1], however if is present the result is always wrapped in an array like first(1) -> [1], not 1.

example

[1,2,3].first() -> 1 [1,2,3].first(2) -> [1,2]

Returns

T in Array<T>

first elements in the array.

public first(num: number): Array<T>

see

first

Parameters

  • num: number

Returns

Array<T>

public flatten(limit?: number): Array<T>

Returns a flattened, one-dimensional copy of the array.

extra

You can optionally specify a [limit], which will only flatten that depth.

example

[[1], 2, [3]].flatten() -> [1,2,3] [['a'],[],'b','c'].flatten() -> ['a','b','c']

Parameters

  • limit?: number optional

    Limit the flattening to this depth, default = Infinity.

Returns

Array<T>

Flattened array.

public from(index: number): Array<T>

Returns a slice of the array from .

example

[1,2,3].from(1) -> [2,3] [1,2,3].from(2) -> [3]

Parameters

  • index: number

    Index to start the slice from.

Returns

Array<T>

Subarray starting from [index] to the end of the original array.

public groupBy(map: string, fn?: (key: string, items: T[]) => void): { [key: string]: T; }

Groups the array by .

extra

Will return an object with keys equal to the grouped values. may be a mapping function, or a string acting as a shortcut. Optionally calls [fn] for each group.

example

['fee','fi','fum'].groupBy('length') -> { 2: ['fi'], 3: ['fee','fum'] } [{age:35,name:'ken'},{age:15,name:'bob'}].groupBy(function(n) { return n.age; }); -> { 35: [{age:35,name:'ken'}], 15: [{age:15,name:'bob'}] }

Parameters

  • map: string

    Property on the elements in the array.

  • fn?: (key: string, items: T[]) => void optional

    Optional callback for each group, default = No callback.

Returns

{ [key: string]: T; }

public groupBy(map: (element: T) => U, fn?: (key: string, items: T[]) => void): { [key: string]: T[]; }

see

groupBy

Parameters

  • map: (element: T) => U

    Callback function for each element, returns the key for the group the element should be in.

  • fn?: (key: string, items: T[]) => void optional

Returns

{ [key: string]: T[]; }

public include(element: T in Array<T>, index?: number): Array<T>

Adds to the array.

extra

This is a non-destructive alias for %add%. It will not change the original array.

example

[1,2,3,4].include(5) -> [1,2,3,4,5] [1,2,3,4].include(8, 1) -> [1,8,2,3,4] [1,2,3,4].include([5,6,7]) -> [1,2,3,4,5,6,7]

Parameters

  • element: T in Array<T>

    Element to add to the array.

  • index?: number optional

    , default = Array.length.

Returns

Array<T>

Array with included at [index].

public include(elements: Array<T>, index?: number): Array<T>

see

include

Parameters

  • elements: Array<T>

    Elements to include into the array at [index].

  • index?: number optional

Returns

Array<T>

public insert(el: T in Array<T>, index?: number): Array<T>

see

add

Parameters

  • el: T in Array<T>
  • index?: number optional

Returns

Array<T>

public insert(el: Array<T>, index?: number): Array<T>

see

add

Parameters

  • el: Array<T>
  • index?: number optional

Returns

Array<T>

public intersect(args?: Array<T>): Array<T>

Returns an array containing the elements all arrays have in common.

extra

This method will also correctly operate on arrays of objects.

example

[1,3,5].intersect([5,7,9]) -> [5] ['a','b'].intersect('b','c') -> ['b']

Parameters

  • args?: Array<T> optional

    Elements to intersect with.

Returns

Array<T>

An array containing the intersecting elements.

public isEmpty(): boolean

Returns true if the array is empty.

extra

This is true if the array has a length of zero, or contains only %undefined%, %null%, or %NaN%.

example

[].isEmpty() -> true [null,undefined].isEmpty() -> true

Returns

boolean

True if the array is empty, otherwise false.

public last(): T in Array<T>

Returns the last element(s) in the array.

method

last([num] = 1)

extra

When is passed, returns the last elements in the array.

example

[1,2,3].last() -> 3 [1,2,3].last(2) -> [2,3]

Returns

T in Array<T>

The last element in the array, if [num] is present then it returns an array of the last [num] elements.

public last(num: number): Array<T>

see

last

Parameters

  • num: number

    The number of elements to return.

Returns

Array<T>

public lastIndexOf(search: any, fromIndex?: number): number

Searches the array and returns the last index where occurs, or -1 if the element is not found.

extra

[fromIndex] is the index from which to begin the search. This method performs a simple strict equality comparison on .

example

[1,2,1].lastIndexOf(1) -> 2 [1,2,1].lastIndexOf(7) -> -1

Parameters

  • search: any

    The element to search for.

  • fromIndex?: number optional

    Start the search from this index, default = Array.length.

Returns

number

The last index of or -1 if is not found.

public least(map: string): Array<T>

Returns the elements in the array with the least commonly occuring value.

method

least([map])

extra

[map] may be a function mapping the value to be checked or a string acting as a shortcut.

example

[3,2,2].least() -> [3] ['fe','fo','fum'].least('length') -> ['fum'] [{age:35,name:'ken'},{age:12,name:'bob'},{age:12,name:'ted'}].least(function(n) { return n.age; }); -> [{age:35,name:'ken'}]

Parameters

  • map: string

    Property on elements in the array.

Returns

Array<T>

Array

public least(map: (n: T) => U): Array<T>

see

least

Parameters

  • map: (n: T) => U

    Callback to retrieve the 'least' property to compare elements against.

Returns

Array<T>

public map(map: string, scope?: any): Array<U>

Maps the array to another array containing the values that are the result of calling on each element.

extra

[scope] is the %this% object. In addition to providing this method for browsers that don't support it natively, this enhanced method also directly accepts a string, which is a shortcut for a function that gets that property (or invokes a function) on each element.

example

[1,2,3].map(function(n) { return n * 3; }); -> [3,6,9] ['one','two','three'].map(function(n) { return n.length; }); -> [3,3,5] ['one','two','three'].map('length') -> [3,3,5]

Parameters

  • map: string

    Property on each element in the array or callback function.

  • scope?: any optional

    This pointer in callback.

Returns

Array<U>

Mapped array.

public map(map: (n: T) => U, scope?: any): Array<U>

see

map

Parameters

  • map: (n: T) => U

    Callback function to map each element in the array.

  • scope?: any optional

Returns

Array<U>

public max(map?: string): T in Array<T>

Returns the element in the array with the greatest value.

method

max([map], [all] = false)

extra

[map] may be a function mapping the value to be checked or a string acting as a shortcut. If [all] is true, will return all max values in an array.

example

[1,2,3].max() -> 3 ['fee','fo','fum'].max('length') -> 'fee' ['fee','fo','fum'].max('length', true) -> ['fee'] [{a:3,a:2}].max(function(n) { return n['a']; }); -> {a:3}

Parameters

  • map?: string optional

    Property on each element in the array or callback function.

Returns

T in Array<T>

Maximum element in the array.

public max(map: string, all: boolean): Array<T>

see

max

Parameters

  • map: string
  • all: boolean

Returns

Array<T>

All max values in the array.

public max(map: (n: T) => U): T in Array<T>

see

max

Parameters

  • map: (n: T) => U

    Callback function to determine the max value of each element in the array.

Returns

T in Array<T>

public max(map: (n: T) => U, all: boolean): Array<T>

see

max

Parameters

  • map: (n: T) => U
  • all: boolean

Returns

Array<T>

public min(map?: string): T in Array<T>

Returns the element in the array with the lowest value.

extra

[map] may be a function mapping the value to be checked or a string acting as a shortcut. If [all] is true, will return all min values in an array.

example

[1,2,3].min() -> 1 ['fee','fo','fum'].min('length') -> 'fo' ['fee','fo','fum'].min('length', true) -> ['fo'] ['fee','fo','fum'].min(function(n) { return n.length; }); -> ['fo'] [{a:3,a:2}].min(function(n) { return n['a']; }); -> [{a:2}]

Parameters

  • map?: string optional

    Property on each element in the array or callback.

Returns

T in Array<T>

Minimum element in the array.

public min(map: string, all: boolean): Array<T>

see

min If true return all min values in the array, default = false.

Parameters

  • map: string
  • all: boolean

Returns

Array<T>

public min(map: (n: T) => U): T in Array<T>

see

min

Parameters

  • map: (n: T) => U

    Callback function to determine the min value of each element in the array.

Returns

T in Array<T>

public min(map: (n: T) => U, all: boolean): Array<T>

see

min

Parameters

  • map: (n: T) => U
  • all: boolean

Returns

Array<T>

public most(map?: string): Array<T>

Returns the elements in the array with the most commonly occuring value.

extra

[map] may be a function mapping the value to be checked or a string acting as a shortcut.

example

[3,2,2].most() -> [2] ['fe','fo','fum'].most('length') -> ['fe','fo'] [{age:35,name:'ken'},{age:12,name:'bob'},{age:12,name:'ted'}].most(function(n) { return n.age; }); -> [{age:12,name:'bob'},{age:12,name:'ted'}]

Parameters

  • map?: string optional

    Property on each element in the array or

Returns

Array<T>

Array of elements that have the most common property.

public most(map: (n: T) => U): Array<T>

see

most

Parameters

  • map: (n: T) => U

    Callback function to determine the element that contains the most commonly occuring value.

Returns

Array<T>

public none(f: T in Array<T>): boolean

Returns true if none of the elements in the array match .

extra

will match a string, number, array, object, or alternately test against a function or regex. This method implements @array_matching.

example

[1,2,3].none(5) -> true ['a','b','c'].none(/b/) -> false [{a:1},{b:2}].none(function(n) { return n['a'] > 1; }); -> true

Parameters

  • f: T in Array<T>

    Value to see if it exists in the array.

Returns

boolean

True if none of the values match , false if one or more match .

public none(f: (n: T) => boolean): boolean

see

none

Parameters

  • f: (n: T) => boolean

    Callback function to determine if none of the elements in the array contain a certain value.

Returns

boolean

public randomize(): Array<T>

Returns a copy of the array with the elements randomized.

extra

Uses Fisher-Yates algorithm.

example

[1,2,3,4].randomize() -> [?,?,?,?]

Returns

Array<T>

Copy of the array with the elements in a random order.

public reduce(fn: (a: T, b: T) => T, init: T in Array<T>): T in Array<T>

Reduces the array to a single result.

extra

If [init] is passed as a starting value, that value will be passed as the first argument to the callback. The second argument will be the first element in the array. From that point, the result of the callback will then be used as the first argument of the next iteration. This is often refered to as "accumulation", and [init] is often called an "accumulator". If [init] is not passed, then will be called n - 1 times, where n is the length of the array. In this case, on the first iteration only, the first argument will be the first element of the array, and the second argument will be the second. After that callbacks work as normal, using the result of the previous callback as the first argument of the next. This method is only provided for those browsers that do not support it natively.

example

[1,2,3,4].reduce(function(a, b) { return a - b; }); [1,2,3,4].reduce(function(a, b) { return a - b; }, 100);

Parameters

  • fn: (a: T, b: T) => T

    Reduce function callback.

  • init: T in Array<T>

    First argument to the callback if present.

Returns

T in Array<T>

Reduced value of the entire array.

public reduceRight(fn: (a: T, b: T) => T, init: T in Array<T>): T in Array<T>

Identical to %Array#reduce%, but operates on the elements in reverse order.

extra

This method is only provided for those browsers that do not support it natively.

example

[1,2,3,4].reduceRight(function(a, b) { return a - b; });

Parameters

  • fn: (a: T, b: T) => T

    Reduce function callback.

  • init: T in Array<T>

    First argument to the callback if present.

Returns

T in Array<T>

Reduced right value of the entire array.

public remove(args?: Array<T>): Array<T>

Removes any element in the array that matches [f1], [f2], etc.

extra

Will match a string, number, array, object, or alternately test against a function or regex. This method will change the array! Use %exclude% for a non-destructive alias. This method implements @array_matching.

example

[1,2,3].remove(3) -> [1,2] ['a','b','c'].remove(/b/) -> ['a','c'] [{a:1},{b:2}].remove(function(n) { return n['a'] == 1; }); -> [{b:2}]

Parameters

  • args?: Array<T> optional

    Elements that should be removed from the array.

Returns

Array<T>

The array with elements removed.

public remove(fn: (n: T) => boolean): Array<T>

see

remove

Parameters

  • fn: (n: T) => boolean

    Callback function to check if elements should be removed.

Returns

Array<T>

public removeAt(start: number, end?: number): Array<T>

Removes element at . If [end] is specified, removes the range between and [end]. This method will change the array! If you don't intend the array to be changed use %clone% first.

example

['a','b','c'].removeAt(0) -> ['b','c'] [1,2,3,4].removeAt(1, 3) -> [1]

Parameters

  • start: number

    Starting index.

  • end?: number optional

    Stop index, default = Array.length.

Returns

Array<T>

Subarray with elements to [end] removed.

public sample(): T in Array<T>

Returns a random element from the array.

extra

If [num] is passed, will return [num] samples from the array.

example

[1,2,3,4,5].sample() -> // Random element [1,2,3,4,5].sample(3) -> // Array of 3 random elements

Returns

T in Array<T>

Single random element in the array.

public sample(num: number): Array<T>

see

sample

Parameters

  • num: number

    Grab this many random elements in the array.

Returns

Array<T>

random elements in the array.

public some(f: T in Array<T>, scope?: any): boolean

Returns true if any element in the array matches .

method

some(, [scope])

extra

[scope] is the %this% object. %any% is provided as an alias. In addition to providing this method for browsers that don't support it natively, this method also implements @array_matching.

example

['a','b','c'].some(function(n) { return n == 'a'; }); ['a','b','c'].some(function(n) { return n == 'd'; }); ['a','b','c'].some('a') -> true [{a:2},{b:5}].some({a:2}) -> true

Parameters

  • f: T in Array<T>

    Element to match or callback function.

  • scope?: any optional

    this object in the callback.

Returns

boolean

Returns true if any element in the array matchs .

public some(f: (n: T) => boolean, scope?: any): boolean

see

some

Parameters

  • f: (n: T) => boolean
  • scope?: any optional

Returns

boolean

public sortBy(map: string, desc?: boolean): Array<T>

Sorts the array by .

extra

may be a function, a string acting as a shortcut, or blank (direct comparison of array values). [desc] will sort the array in descending order. When the field being sorted on is a string, the resulting order will be determined by an internal collation algorithm that is optimized for major Western languages, but can be customized. For more information see @array_sorting.

example

['world','a','new'].sortBy('length') -> ['a','new','world'] ['world','a','new'].sortBy('length', true) -> ['world','new','a'] [{age:72},{age:13},{age:18}].sortBy(function(n) { return n.age; }); -> [{age:13},{age:18},{age:72}]

Parameters

  • map: string

    Property on each element in the array or map callback function.

  • desc?: boolean optional

    True to sort the array in descending order, default = false.

Returns

Array<T>

Sorted array.

public sortBy(fn: (n: T) => U, desc?: boolean): Array<T>

see

sortBy

Parameters

  • fn: (n: T) => U
  • desc?: boolean optional

Returns

Array<T>

public subtract(args?: Array<T>): Array<T>

Subtracts from the array all elements in [a1], [a2], etc.

extra

This method will also correctly operate on arrays of objects.

example

[1,3,5].subtract([5,7,9]) -> [1,3] [1,3,5].subtract([3],[5]) -> [1] ['a','b'].subtract('b','c') -> ['a']

Parameters

  • args?: Array<T> optional

    Elements to remove from the array.

Returns

Array<T>

Array with removed.

public subtract(args: Array<T>): Array<T>

see

subtract

Parameters

  • args: Array<T>

Returns

Array<T>

public sum(map: string): number

Sums all values in the array.

extra

[map] may be a function mapping the value to be summed or a string acting as a shortcut.

example

[1,2,2].sum() -> 5 [{age:35},{age:12},{age:12}].sum(function(n) { return n.age; }); -> 59 [{age:35},{age:12},{age:12}].sum('age') -> 59

Parameters

  • map: string

    Property on each element in the array or callback function to sum up the elements.

Returns

number

The sum of all elements in the array.

public sum(map: (n: T) => number): number

see

sum

Parameters

  • map: (n: T) => number

Returns

number

public to(index: number): Array<any>

Returns a slice of the array up to .

example

[1,2,3].to(1) -> [1] [1,2,3].to(2) -> [1,2]

Parameters

  • index: number

    Slick the array up to this index.

Returns

Array<any>

Slice of the array from 0 to .

public union(array: Array<T>): Array<T>

Returns an array containing all elements in all arrays with duplicates removed.

method

union([a1], [a2], ...)

extra

This method will also correctly operate on arrays of objects.

example

[1,3,5].union([5,7,9]) -> [1,3,5,7,9] ['a','b'].union(['b','c']) -> ['a','b','c']

Parameters

  • array: Array<T>

Returns

Array<T>

Union between the original array and .

public union(args?: Array<T>): Array<T>

see

union

Parameters

  • args?: Array<T> optional

    Elements to create a union with.

Returns

Array<T>

Union between the original array and

public unique(map?: string): Array<T>

Removes all duplicate elements in the array.

extra

[map] may be a function mapping the value to be uniqued on or a string acting as a shortcut. This is most commonly used when you have a key that ensures the object's uniqueness, and don't need to check all fields. This method will also correctly operate on arrays of objects.

example

[1,2,2,3].unique() -> [1,2,3] [{foo:'bar'},{foo:'bar'}].unique() -> [{foo:'bar'}] [{foo:'bar'},{foo:'bar'}].unique(function(obj){ return obj.foo; }); -> [{foo:'bar'}] [{foo:'bar'},{foo:'bar'}].unique('foo') -> [{foo:'bar'}]

Parameters

  • map?: string optional

    Property on each element in the array or callback function to pick a property on the element.

Returns

Array<T>

Array with only unique elements.

public unique(fn: (obj: T) => U): Array<T>

see

unique

Parameters

  • fn: (obj: T) => U

    Callback to pluck the property to check for uniqueness.

Returns

Array<T>