Javascript Lodash 映射对象上的键和值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45358241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lodash map keys and values on an object
提问by danday74
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 方法来做到这一点?
回答by Ori Drori
This solution uses _.transform(), and it's a bit shorter. I'm not sure that it's better than your functional solution.
此解决方案使用_.transform(),并且它有点短。我不确定它是否比您的功能解决方案更好。
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, mapthem 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);
回答by Yahya Uddin
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. It's not a big issue, but adds to the number of lines. An alternative approach is as follows
上述方法的主要问题之一是您需要使用 reduce 语句返回结果。这不是一个大问题,但增加了行数。另一种方法如下
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);
回答by mindlis
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>
回答by danday74
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 变换在这里似乎更合适

