4.17.11
3.10.1
2.4.2
1.3.1
_.iteratee([func=_.identity])
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
argument
[func=_.identity]
type
*
description
The value to convert to a callback.
Returns
(array)
Example
var users = [
{
user: "barney",
age: 36,
active: true,
},
{
user: "fred",
age: 40,
active: false,
},
];
// The `_.matches` iteratee shorthand.
_.filter(
users,
_.iteratee({
user: "barney",
active: true,
})
);
// => [{ 'user': 'barney', 'age': 36, 'active': true }]
// The `_.matchesProperty` iteratee shorthand.
_.filter(
users,
_.iteratee(["user", "fred"])
);
// => [{ 'user': 'fred', 'age': 40 }]
// The `_.property` iteratee shorthand.
_.map(users, _.iteratee("user"));
// => ['barney', 'fred']
// Create custom iteratee shorthands.
_.iteratee = _.wrap(
_.iteratee,
function (iteratee, func) {
return !_.isRegExp(func)
? iteratee(func)
: function (string) {
return func.test(string);
};
}
);
_.filter(["abc", "def"], /ef/);
// => ['def']
var users = [
{ user: "barney", age: 36, active: true },
{ user: "fred", age: 40, active: false },
];
// The `_.matches` iteratee shorthand.
_.filter(users, _.iteratee({ user: "barney", active: true }));
// => [{ 'user': 'barney', 'age': 36, 'active': true }]
// The `_.matchesProperty` iteratee shorthand.
_.filter(users, _.iteratee(["user", "fred"]));
// => [{ 'user': 'fred', 'age': 40 }]
// The `_.property` iteratee shorthand.
_.map(users, _.iteratee("user"));
// => ['barney', 'fred']
// Create custom iteratee shorthands.
_.iteratee = _.wrap(_.iteratee, function (iteratee, func) {
return !_.isRegExp(func)
? iteratee(func)
: function (string) {
return func.test(string);
};
});
_.filter(["abc", "def"], /ef/);
// => ['def']