Javascript 迭代对象属性并修改它们
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26013802/
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
Iterate over object attributes and modify them
提问by ducin
Underscore.js provides _.eachand _.mapon collections, which is nice, but I need to iterate over all attributes of my object. I need to modify the values and preserve the keys. E.g. I've got something like: {a:1, b:2, c:3}and I need to perform an operation that changes the value but keeps the keys. Let's say, I'll calculate squares, I should get {a:1, b:4, c:9}. The question is: how to do that using underscore(not interested in vanilla javascript)? I'd love a method like:
Underscore.js 提供_.each和_.map集合,这很好,但我需要遍历对象的所有属性。我需要修改值并保留密钥。例如,我有类似的东西:{a:1, b:2, c:3}我需要执行一个更改值但保留密钥的操作。比方说,我会计算平方,我应该得到{a:1, b:4, c:9}。问题是:如何使用下划线(对 vanilla javascript 不感兴趣)来做到这一点?我喜欢这样的方法:
var a = {a:1, b:2, c:3}
_.magic(a, function(item){ return item*item; });
Additionally, it would be great if this was possible to chain it, since I'm doing a map, dump result to perform each and then use a map again (because I need to).
此外,如果可以链接它会很棒,因为我正在制作地图,转储结果以执行每个然后再次使用地图(因为我需要)。
采纳答案by Javier92
I looked a little bit more into some of my snippets to see if there was an option to mutate the original object, but I didn't find anything interesting, so I'd go with this:\
我更仔细地查看了我的一些片段,看看是否有一个选项可以改变原始对象,但我没有发现任何有趣的东西,所以我会选择这个:\
var original = {a:1, b:2, c:3};
var squaredValues = _.object(_.map(original, function (value, key) {
return [key, value * value];
}));
I'm hoping there's a more elegant solution though.
我希望有一个更优雅的解决方案。
回答by Justin
_.mapObject(added in version 1.8) is exactly what you're looking for.
_.mapObject(在 1.8 版中添加)正是您正在寻找的。
For previous versions, can mutate the original object using the third argument to the _.eachcallback.
对于以前的版本,可以使用_.each回调的第三个参数来改变原始对象。
_.each({a:1, b:2, c:3}, function(value, key, obj) { obj[key] = value * value; });
Using _.reducewith an initial memo can create a brand new object as well.
使用_.reduce与初始备忘录可以创建一个全新的对象也是如此。
_.reduce({a:1, b:2, c:3}, function(memo, value, key) {
memo[key] = value * value;
return memo;
}, {});
I'd prefer the _.reduceapproach. A simple mixin will make it behave exactly like _.map.
我更喜欢这种_.reduce方法。一个简单的 mixin 将使它的行为与_.map.
_.mixin({
magic: function(obj, iterator, context) {
return _.reduce(obj, function(memo, value, key) {
memo[key] = iterator.call(context, value, key, obj);
return memo;
}, {});
}
});
回答by estani
As Justin said _.mapObjectmightbe what you were looking for. Butthis creates a new object (extending {}) which might not be what you want (e.g. if it's not a simple object like new THREE.Vector3()), Here it's no good to replace the original object with something different.
正如贾斯汀所说_.mapObject可能是你正在寻找的。但这会创建一个新对象(扩展{}),它可能不是您想要的(例如,如果它不是像 那样的简单对象new THREE.Vector3()),在这里用不同的东西替换原始对象是不好的。
For that case (modifying values in place inthe given object) just use _.eachwith the third parameter of your iteratee function:
对于这种情况(在就地修改值在给定对象),只要用_.each与iteratee功能的第三个参数:
_.each(complexObject, function(value, key, obj) {
obj[key] = value * value;
});
回答by ducin
Big thanks to Javier92 who pointed me to the right solution.
非常感谢 Javier92,他为我指出了正确的解决方案。
Anyway, I don't like to use an underscore method that uses another two underscore methods (it's not readable that much), so I came up with a simple wrapper function that is mixed in into underscore:
无论如何,我不喜欢使用使用另外两个下划线方法的下划线方法(它不那么可读),所以我想出了一个简单的包装函数,它混合到下划线中:
// mixin to make our new function available via _
_.mixin({
updateAttributes: function(object, iteratee) {
return _.object(_.map(object, function (value, key) {
return [key, iteratee(value)];
}));
}
});
use it somewhere in your code:
在代码中的某处使用它:
_.updateAttributes({a:1, b:2, c:3}, function(item){ return item*item; })
// Object {a: 1, b: 4, c: 9}
couldn't think of a one-word function (probably there's something better than updateAttributes).
想不出一个词的功能(可能有比 更好的东西updateAttributes)。

