最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论