_.second(['a','b']);
Functions to take things from arrays. View Annotated Source
The _.second
function is a convenience for the equivalent array[1]
:
_.second(['a','b']);
=> 'b'
_.map([['a','b'], _.range(10,20)], _.second);
=> ['b',11]
You can also pass an optional number to the _.second
function to take a number of elements from an array starting with the second element and ending at the given index:
_.second(_.range(10), 5)
_.third(['a','b','c']);
=> 'c'
_.map([['a','b','c'], _.range(10,20)], _.third);
=> ['c',12]
You can also pass an optional number to the _.third
function to take a number of elements from an array starting with the third element and ending at the given index:
_.third(_.range(10), 5)
_.nth(['a','b','c'], 2);
_.nth(['a','b','c'], 2000);
var b = [['a'],['b'],[]];
_.compact(_.map(b, function(e) { return _.nth(e,0) }));
=> ['a','b']
If wrapping a function around _.nth
is too tedious or you'd like to partially apply the index then lodash-contrib offers any of _.flip2
, _.fix
or rcurry2
to solve this.
The _.takeWhile
function takes an array and a function and returns a new array containing the first n elements in the original array for which the given function returns a truthy value:
var isNeg = function(n) { return n < 0; };
_.takeWhile([-2,-1,0,1,2], isNeg);
=> [-2,-1]
The _.dropWhile
function works similarly except that it drops elements from the original array for which the given function returns a truthy value:
_.dropWhile([-2,-1,0,1,2], isNeg);
=> [0,1,2]