Underscore.js provides _.each
and _.map
on 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:
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).