amd-utils / array

Array utilities.

Table of Contents #

append(arr1, arr2):Array #

Appends an array to the end of the other. The first array will be modified and will contain the appended items.

See: union(), combine()


var foo = ['a', 'b'],
    bar = ['b', 'd'];

append(foo, bar); // ['a', 'b', 'b', 'd']

combine(arr1, arr2):Array #

Combines an array with all the items of another. The first array will be modified and will contain the combined items. Does not allow duplicates and is case and type sensitive.

See: union(), append()


var foo = ['a', 'b'],
    bar = ['b', 'd'];

combine(foo, bar); // ['a', 'b', 'd']

compact(arr):Array #

Returns a new Array without any null or undefined values. Note that it will keep empty strings and other falsy values (simillar to Ruby Array#compact).


var arr = [0, 1, null, false, '', 'foo', undefined, 'bar'];
compact(arr); // [0, 1, false, '', 'foo', 'bar'];

contains(arr, value):Boolean #

Checks if Array contains value. Alias to indexOf(arr, val) !== -1.


var arr = [1, 2, 3];
contains(arr, 2);      // true
contains(arr, 'foo');  // false

difference(...arrs):Array #

Return a new Array with elements that aren't present in the other Arrays besides the first one.

Works like Python set#difference.

It will remove duplicates.

See: xor(), intersection()


var a = ['a', 'b', 1];
var b = ['c', 1];
difference(a, b); // ['a', 'b']

every(arr, callback, [thisObj]):Array #

Crossbrowser ES5 Array.every().

Tests whether all elements in the array pass the test implemented by the provided function.

more info at MDN Array#every

filter(arr, callback, [thisObj]):Array #

Crossbrowser ES5 Array.filter().

Creates a new array with all elements that pass the callback test.

more info at MDN Array#filter

find(arr, callback, [thisObj]):void #

Loops through all the items in the Array and returns the first one that passes a truth test (callback).

var arr = [123, {a:'b'}, 'foo', 'bar'];
find(arr, isString); // "foo"
find(arr, isNumber); // 123
find(arr, isObject); // {a:'b'}

forEach(arr, callback, [thisObj]):void #

Crossbrowser ES5 Array.forEach().

more info at MDN Array#forEach

indexOf(arr, item, [fromIndex]):Number #

Crossbrowser ES5 Array.indexOf().

more info at MDN Array#indexOf

insert(arr, ...items):Number #

Push items into array only if they aren't contained by it. Returns the new length of the array.

See: remove(), removeAll(), contains()


var arr = ['a', 'b'];
insert(arr, 'a');       // 2 : ['a', 'b']
insert(arr, 'c');       // 3 : ['a', 'b', 'c']
insert(arr, 1, 2, 'b'); // 5 : ['a', 'b', 'c', 1, 2]

intersection(...arrs):Arrays #

Return a new Array with elements common to all Arrays.

Similar to Python set#intersection and underscore.js intersection.

It will remove duplicates.

See: difference(), xor()


var a = ['a', 'b', 1],
    b = ['c', 1],
    c = [1, 2, 3];
intersection(a, b, c); // [1]

lastIndexOf(arr, item, [fromIndex]):Number #

Crossbrowser ES5 Array.lastIndexOf().

more info at MDN Array#lastIndexOf

map(arr, callback):Array #

Crossbrowser ES5 Array.map().

Creates a new array with the results of calling a provided function on every element in this array.

more info at MDN Array#map

max(arr[, iterator]):* #

Returns maximum value inside array or use a custom iterator to define how items should be compared.

See: min()


max([10, 2, 7]); // 10
max(['foo', 'lorem', 'amet'], function(val){
    return val.length;
}); // 'lorem'

min(arr[, iterator]):* #

Returns minimum value inside array or use a custom iterator to define how items should be compared.

See: max()


min([10, 2, 7]); // 2
min(['foo', 'lorem', 'amet'], function(val){
    return val.length;
}); // 'foo'

range([start], stop[, step]):Array #

Creates a new Array with all the values inside the range. Works similarly to Python#range or PHP#range.

Arguments

  1. [start] (Number) : Range start. Default is 0.
  2. stop (Number) : Range limit.
  3. [step] (Number) : Step size. Default is 1.

Example


range(5);         // [0, 1, 2, 3, 4, 5]
range(0, 5);      // [0, 1, 2, 3, 4, 5]
range(0, 5, 2);   // [0, 2, 4]
range(20, 40, 5); // [20, 25, 30, 35, 40]

reduce(arr, fn):* #

Crossbrowser ES5 Array.reduce().

Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.

more info at MDN Array#reduce

reduceRight(arr, fn):* #

Crossbrowser ES5 Array.reduceRight().

Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.

more info at MDN Array#reduceRight

remove(arr, item):void #

Remove a single item from the array.

IMPORTANT: it won't remove duplicates, just a single item.

Example


var foo = [1, 2, 3, 4];
remove(foo, 2);
console.log(foo); // [1, 3, 4]

removeAll(arr, item):void #

Remove all instances of an item from the array.

Example


var foo = [1, 2, 3, 4, 2, 2];
removeAll(foo, 2);
console.log(foo); // [1, 3, 4];

shuffle(arr):Array #

Returns a new Array with items randomly sorted (shuffled). Similar to Ruby Array#shuffle.

Example


var arr = ['a', 'b', 'c', 'd', 'e'];
shuffle(arr); // ['b', 'd', 'e', 'c', 'a']

some(arr, callback, [thisObj]):Array #

Crossbrowser ES5 Array.some().

Tests whether some element in the array passes the test implemented by the provided function.

more info at MDN Array#some

union(...arrs):Array #

Concat multiple arrays removing duplicates.


var a = ['a', 'b'],
    b = ['c', 'a'],
    c = [1, 'b', 2, 3, 'a'];

//note that unique remove from begin to end
union(a, b, c); // ['c', 1, 'b', 2, 3, 'a']

unique(arr):Array #

Return a new Array of unique items.

Example


var foo = [1, 2, 3, 4, 2, 2, 3, 4];
var bar = unique(foo);
console.log(foo); // [1, 2, 3, 4];

xor(arr1, arr2):Array #

Exclusive OR. Returns items that are present in a single array.

Works like Python set#symmetric_difference renamed for brevity.

It will remove duplicates.

See: difference(), intersection()


var a = ['a', 'b', 1];
var b = ['c', 1];
xor(a, b); // ['a', 'b', 'c']

For more usage examples check specs inside /tests folder. Unit tests are the best documentation you can get...


Documentation generated by mdoc.