Lodash _.mapValues

2021-09-23 10:43 更新

_.mapValues(object, [iteratee=_.identity])

創(chuàng)建一個(gè)對(duì)象,這個(gè)對(duì)象的key與object對(duì)象相同,值是通過 iteratee 運(yùn)行 object 中每個(gè)自身可枚舉屬性名字符串產(chǎn)生的。 iteratee調(diào)用三個(gè)參數(shù): (value, key, object)。

添加版本

2.4.0

參數(shù)

  1. object (Object): 要遍歷的對(duì)象。
  2. [iteratee=_.identity] (Function): 每次迭代時(shí)調(diào)用的函數(shù)。

返回

(Object): 返回映射后的新對(duì)象。

例子

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)


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)