Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 542
KajMagnus Avatar asked Jun 11 '12 02:06

KajMagnus


People also ask

What is Map object in JavaScript?

The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.

What is Map in JavaScript with example?

Definition and Usage. map() creates a new array from calling a function for every array element. map() calls a function once for each element in an array. map() does not execute the function for empty elements. map() does not change the original array.

How do you Map an array to an object?

To convert an array of objects to a Map , call the map() method on the array and on each iteration return an array containing the key and value. Then pass the array of key-value pairs to the Map() constructor to create the Map object.

How do I iterate a Map in JavaScript?

Iterate through a Map using JavaScript # Use the forEach() method to iterate over a Map object. The forEach method takes a function that gets invoked for each key/value pair in the Map , in insertion order. The function gets passed the value, key and the Map object on each iteration.

What is a map object in JavaScript?

Map is a collection of elements where each element is stored as a Key, value pair. Map object can hold both objects and primitive values as either key or value. When we iterate over the map object it returns the key,value pair in the same order as inserted. Map.prototype.size – It returns the number of elements or the key-value pairs in the map.

What is the difference between map () and transform () function in JavaScript?

It is similar to the JavaScript map () function but the values got replaced in transform () function. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.

What is a CoffeeScript object?

In short, CoffeeScript objects are a collection of key-value pairs. The objects are defined using curly braces, an empty object is represented as {}.

What is the difference between map () and callback () in JavaScript?

The syntax for the map () method is as follows: arr.map (function (element, index, array) { }, this); The callback function () is called on each array element, and the map () method always passes the current element, the index of the current element, and the whole array object to it. The this argument will be used inside the callback function.


1 Answers

If you want to map an object to an object, you need to use a fold (traditional functional terminology) or reduce (common 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 commonly 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 comprehensions always return lists, even when iterating over an object. So you might want to look into the CoffeeScript version of Underscore.

like image 131
Mark Reed Avatar answered Sep 27 '22 17:09

Mark Reed