主题
iteratee
js
_.iteratee([func=_.identity])
创建一个函数,使用创建函数的参数调用 func
。如果 func
是属性名称,则创建的函数返回给定元素的属性值。如果 func
是数组或对象,则创建的函数返回包含等效源属性的元素的 true
,否则返回 false
。
¥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
[func=_.identity]
(*):要转换为回调的值。¥
[func=_.identity]
()*: The value to convert to a callback.
返回
¥Returns
(函数):返回回调。
¥(Function): Returns the callback.
示例
¥Example
js
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']