跳到主要内容

iteratee

_.iteratee(value, [context])
生成可应用于集合中的每个元素的回调。_.iteratee 支持许多常见回调用例的简写语法。根据值的类型,_.iteratee 将返回:

// No value
_.iteratee();
=> _.identity()

// Function
_.iteratee(function(n) { return n * 2; });
=> function(n) { return n * 2; }

// Object
_.iteratee({firstName: 'Chelsea'});
=> _.matcher({firstName: 'Chelsea'});

// Anything else
_.iteratee('firstName');
=> _.property('firstName')

通过_.iteratee 转换判断的 Underscore 方法的完整列表是: countBy, every,filter, find, findIndex, findKey,findLastIndex, groupBy, indexBy,map, mapObject, max, min,partition, reject, some, sortBy,sortedIndex, and uniq

如果您需要其他或不同的简写语法,可以使用自己的自定义函数覆盖 _.iteratee

// Support `RegExp` predicate shorthand.
var builtinIteratee = _.iteratee;
_.iteratee = function(value, context) {
if (_.isRegExp(value)) return function(obj) { return value.test(obj) };
return builtinIteratee(value, context);
}