Skip to main content

iteratee

_.iteratee(func) # Ⓢ [Ⓣ][1] Ⓝ

Creates a function that invokes func with the arguments of the created



function. If func is a property name, the created function returns the



property value for a given element. If func is an array or object, the



created function returns true for elements that contain the equivalent



source properties, otherwise it returns false.

Since​

4.0.0

Arguments​

  1. func (*): The value to convert to a callback.

Returns​

(Function): Returns the callback.

Example​

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

// The `_.matches` iteratee shorthand.
_.filter(_.iteratee({ 'user': 'barney', 'active': true }), users);
// => [{ 'user': 'barney', 'age': 36, 'active': true }]

// The `_.matchesProperty` iteratee shorthand.
_.filter(_.iteratee(['user', 'fred']), users);
// => [{ 'user': 'fred', 'age': 40 }]

// The `_.property` iteratee shorthand.
_.map(_.iteratee('user'), users);
// => ['barney', 'fred']

// Create custom iteratee shorthands.
_.iteratee = _.wrap(function(iteratee, func) {
return !_.isRegExp(func) ? iteratee(func) : function(string) {
return func.test(string);
};
}, _.iteratee);

_.filter(/ef/, ['abc', 'def']);
// => ['def']