_.mapValues
语法:
_.mapValues(object, [iteratee=_.identity])
源代码链接:source
npm包链接:npm package
描述:
创建一个对象,这个对象的key与object
对象相同,值是通过iteratee
运行object
中每个自身可枚举属性名字符串产生的。iteratee
调用三个参数:(value, key, object)。
开始版本:2.4.0
参数:
object (Object)
: 要遍历的对象。[iteratee=_.identity] (Function)
: 每次迭代时调用的函数。
返回值:
(Object)
: 返回映射后的新对象。
例子:
var users = {
'fred': { 'user': 'fred', 'age': 40 },
'pebbles': { 'user': 'pebbles', 'age': 1 }
};
_.mapValues(users, function(o) { return o.age; });
// => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
// The `_.property` iteratee shorthand.
_.mapValues(users, 'age');
// => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
Last updated
Was this helpful?