takeWhile source npm

_.takeWhile(array, [predicate=_.identity])

Creates a slice of array with elements taken from the beginning. Elements are taken until predicate returns falsey. The predicate is invoked with three arguments: (value, index, array).

Arguments

  1. array (Array)

    The array to query.

  2. [predicate=_.identity] (Function|Object|string)

    The function invoked per iteration.

Returns (Array)

Returns the slice of array.

Example

var resolve = _.partial(_.map, _, 'user');

var users = [
  { 'user': 'barney',  'active': false },
  { 'user': 'fred',    'active': false},
  { 'user': 'pebbles', 'active': true }
];

resolve( _.takeWhile(users, function(o) { return !o.active; }) );
// => ['barney', 'fred']

// using the `_.matches` callback shorthand
resolve( _.takeWhile(users, { 'user': 'barney', 'active': false }) );
// => ['barney']

// using the `_.matchesProperty` callback shorthand
resolve( _.takeWhile(users, ['active', false]) );
// => ['barney', 'fred']

// using the `_.property` callback shorthand
resolve( _.takeWhile(users, 'active') );
// => []