主题
mapValues
js
_.mapValues(object, [iteratee=_.identity])
创建一个具有与 object
相同的键的对象,并通过运行 object
到 iteratee
的每个可枚举字符串键属性生成值。迭代器使用三个参数调用:(value, key, object)。
¥Creates an object with the same keys as object
and values generated by running each own enumerable string keyed property of object
thru iteratee
. The iteratee is invoked with three arguments: (value, key, object).
新增于
¥Since
2.4.0
参数
¥Arguments
object
(对象):要迭代的对象。¥
object
(Object): The object to iterate over.[iteratee=_.identity]
(函数):每次迭代调用的函数。¥
[iteratee=_.identity]
(Function): The function invoked per iteration.
返回
¥Returns
(对象):返回新的映射对象。
¥(Object): Returns the new mapped object.
示例
¥Example
js
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)