简体   繁体   English

Lodash 映射对象上的键和值

[英]Lodash map keys and values on an object

My code:我的代码:

const orig = {" a ":1, " b ":2}
let result = _.mapKeys(_.mapValues(orig, (v) => v + 1), (v, k) => k.trim())

actual and desired result = { "a": 2, "b": 3 }实际和期望的结果 = { "a": 2, "b": 3 }

But is there a better Lodashy way of doing this?但是有没有更好的 Lodashy 方法来做到这一点?

This solution uses _.transform() , and it's a bit shorter.这个解决方案使用_.transform() ,它有点短。 I'm not sure that it's better than your functional solution.我不确定它是否比您的功能解决方案更好。

 const orig = {" a ": 1, " b ": 2 }; const result = _.transform(orig, (r, v, k) => { r[k.trim()] = v + 1; }); console.log(result);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Without lodash I would use Object.entries() to extract the key and value, map them to the required forms, and then use Object.fromEntries() to convert back to an object:如果没有 lodash,我将使用Object.entries()提取键和值, 它们映射到所需的表单,然后使用Object.fromEntries()转换回对象:

 const orig = {" a ": 1, " b ": 2 }; const result = Object.fromEntries(Object.entries(orig).map(([k, v]) => [k.trim(), v + 1])); console.log(result);

Just for completeness, this is how you would do it without lodash:为了完整起见,这就是没有 lodash 的方法:

Solution 1: Object.keys & reduce解决方案 1:Object.keys & reduce

 const orig = {" a ":1, " b ":2}; let result = Object.keys(orig).reduce((r, k) => { r[k.trim()] = orig[k] + 1; return r; }, {}) console.log(result);

Solution 2: Object.entries & reduce解决方案 2:Object.entries & reduce

If your okay with using a polyfill for IE, you can also do this:如果您同意为 IE 使用 polyfill,您也可以这样做:

 const orig = {" a ":1, " b ":2}; let result = Object.entries(orig).reduce((r, [k, v]) => { r[k.trim()] = v + 1; return r; }, {}) console.log(result);

Solution 3: Object.keys/ Object.entries with Array.forEach解决方案 3:Object.keys/Object.entries 和 Array.forEach

One of the main issues with the above approaches is that you need to return result with reduce statements.上述方法的主要问题之一是您需要使用 reduce 语句返回结果。 It's not a big issue, but adds to the number of lines.这不是一个大问题,但增加了行数。 An alternative approach is as follows另一种方法如下

 const orig = {" a ":1, " b ":2}; let result = {}; Object.keys(orig).forEach(k => result[k.trim()] = orig[k] + 1); console.log(result);

or

 const orig = {" a ":1, " b ":2}; let result = {}; Object.entries(orig).forEach(([k, v]) => result[k.trim()] = v + 1); console.log(result);

How about something like this?这样的事情怎么样?

 const orig = {" a ":1, " b ":2}; function test(object) { let out = {}; _.forEach(object, function(value, key) { out[key.trim()] = value + 1; }); return out; } console.log(test(orig));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Using reduce you can do:使用 reduce 你可以做到:

 var obj = {" a ":1, " b ":2} result = _.reduce(obj, (result, v, k) => { result[k.trim()] = v + 1 return result }, {}) console.log(result)
 <script src="https://cdn.jsdelivr.net/npm/lodash@latest/lodash.min.js"></script>

reduce is more familiar but IMO transform seems more appropriate here reduce 更熟悉,但 IMO 变换在这里似乎更合适

You could use a good ol' for in loop as it looks cleaner你可以使用一个好的 ol' for in循环,因为它看起来更干净

const orig = {" a ":1, " b ":2};
let result = {};
for (const key in orig) {
  result[key.trim()] = orig[key] + 1
}
// result {a: 2, b: 3} 

you could even make it a one liner.. :)你甚至可以把它做成一个衬里.. :)

const orig = { ' a ': 1, ' b ': 2 };
let result = {}; for (const key in orig) result[key.trim()] = orig[key] + 1;
// result {a: 2, b: 3} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM