Any Javascript or Coffeescript `map` function that transforms object *values*? - Stack Overflow

Is there no ubiquitousstandard Javascript or Coffeescript function that Transforms the values of an ob

Is there no ubiquitous/standard Javascript or Coffeescript function that Transforms the values of an object/map/hash?

jQuery has $.map but it produces Arrays only.
Underscore has _.map but it also produces Arrays only.

To be clear, a function like this one is what I'm looking for. (This example is written in Coffeescript not Javascript.)

# Transforms the values in a map. (Doesn't modify `obj` — returns a new map.)
# Example usage:
#   mapObjVals({ a: 'aa', b: 'bb'}, (key, value) -> value + '_!')
#   --> { a: 'aa_!', b: 'bb_!' }
mapObjVals = (obj, f) ->
  obj2 = {}
  for k, v of obj
    obj2[k] = f k, v
  obj2

Is there no ubiquitous/standard Javascript or Coffeescript function that Transforms the values of an object/map/hash?

jQuery has $.map but it produces Arrays only.
Underscore has _.map but it also produces Arrays only.

To be clear, a function like this one is what I'm looking for. (This example is written in Coffeescript not Javascript.)

# Transforms the values in a map. (Doesn't modify `obj` — returns a new map.)
# Example usage:
#   mapObjVals({ a: 'aa', b: 'bb'}, (key, value) -> value + '_!')
#   --> { a: 'aa_!', b: 'bb_!' }
mapObjVals = (obj, f) ->
  obj2 = {}
  for k, v of obj
    obj2[k] = f k, v
  obj2
Share Improve this question asked Jun 11, 2012 at 2:36 KajMagnusKajMagnus 11.7k17 gold badges86 silver badges132 bronze badges 1
  • 1 You seem to have answered your own question :) Just make sure you use for own k, v of obj to prevent being bit by prototype extensions. I would use the for loop directly instead of a helper function, as this is a rare ocurrence. – Ricardo Tomasi Commented Jun 11, 2012 at 4:51
Add a ment  | 

1 Answer 1

Reset to default 6

If you want to map an object to an object, you need to use a fold (traditional functional terminology) or reduce (mon modern name, used by underscore), which builds a new value from a collection:

  _.reduce(obj, function(newObj, thisValue, thisKey) { 
      // modify newObj based on thisKey/thisValue
      return newObj;
      }, {})

The function passed as the second argument is called once per key/value pair. It is passed in the object being built as its first argument, followed by the current value, followed by the associated key. It is up to the function to modify the object and return its new value.

The third argument to _.reduce is the initial value of the new object, to be passed in with the first key/value pair; in this case it's an empty object/map/hash {}.

Reduce/fold/inject is monly used for summing values. Basically, any time you want to construct a new single value from a collection. map is really just a special case of reduce where the allegedly-reduced value is really another collection of the same size as the original.

For CoffeeScript, AFAIK, list prehensions always return lists, even when iterating over an object. So you might want to look into the CoffeeScript version of Underscore.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745667581a4639209.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信