-
Notifications
You must be signed in to change notification settings - Fork 1
Arrays
Darin Haener edited this page Sep 26, 2015
·
7 revisions
##Methods
###compact
Removes all undefined, null, or empty strings from an array
[1, 2, undefined, 3, "", null, 4].compact
-> [1, 2, 3, 4]
###select
Returns a new array of all values for which the predicate returns true
[1, 2, 3, 4, 5].select(value => value % 2 === 0);
-> [2, 4]
###include
Returns true if value is in array, otherwise returns false.
[1, 2, 3, 4, 5].include(4);
-> true
###flatten
Flattens a deeply nested array of arrays
[1, [2, 3, [4, 5, 6]]].flatten;
-> [1, 2, 3, 4, 5, 6]
###any
Returns true if an array is not empty
[1].any
-> true
[].any
-> false
[null, undefined, ""].any
-> false
###empty
Returns true if an array is empty (or if it only contains null, undefined, or empty strings)
[].empty
-> true
[1].empty
-> false
[null, "", undefined].empty
-> true
###first
Gets first x items from an array
var array = ["t", "e", "s", "t"];
array.first() // -> ['t']
array.first(2) // -> ['t', 'e']
###last
Gets last x items from an array
var array = [1,2,3,4];
array.last() // -> [4]
array.first(2) // -> [3,4]
###find
Finds the first value in array for which the predicate returns true, otherwise returns undefined
[1, 2, 3, 4].find((value) => v === 3)
-> 3
[1, 2, 3, 4].find((value) => v === 9)
-> undefined