且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

映射对象保留键

更新时间:2023-02-09 13:00:09

带有下划线

Underscore提供了 _.mapObject 函数来映射值并保留键.

Underscore provides a function _.mapObject to map the values and preserve the keys.

_.mapObject({ one: 1, two: 2, three: 3 }, function (v) { return v * 3; });

// => { one: 3, two: 6, three: 9 }

演示

使用 Lodash

Lodash提供了 _.mapValues 函数来映射值并保留键.

Lodash provides a function _.mapValues to map the values and preserve the keys.

_.mapValues({ one: 1, two: 2, three: 3 }, function (v) { return v * 3; });

// => { one: 3, two: 6, three: 9 }

演​​示