_.transform
语法:
_.transform(object, [iteratee=_.identity], [accumulator])源代码链接:source
npm包链接:npm package
描述:
_.reduce的替代方法;此方法将转换object对象为一个新的accumulator对象,结果来自iteratee处理自身可枚举的属性。 每次调用可能会改变accumulator对象。如果不提供accumulator,将使用与[[Prototype]]相同的新对象。iteratee调用4个参数:(accumulator, value, key, object)。如果返回false,iteratee会提前退出。
开始版本:1.3.0
参数:
object (Object): 要遍历的对象[iteratee=_.identity] (Function): 每次迭代时调用的函数。[accumulator] (*): 定制叠加的值。
返回值:
(*): 返回叠加后的值。
例子:
_.transform([2, 3, 4], function(result, n) {
result.push(n *= n);
return n % 2 == 0;
}, []);
// => [4, 9]
_.transform({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
(result[value] || (result[value] = [])).push(key);
}, {});
// => { '1': ['a', 'c'], '2': ['b'] }Last updated
Was this helpful?