How, using ImmutableJS, do I produce a new map by mapping over the key/value pairs of an input map?
In Scala, I would do something like this:
scala> Map(1->2, 3->4).toSeq.map{case (k, v) => (k*k) -> (v*v*v)}.toMap
res1: scala.collection.immutable.Map[Int,Int] = Map(1 -> 8, 9 -> 64)
(This case trivially squares the key and cubes the value.)
In JavaScript, I'm hoping for something like:
Immutable.fromJS({1: 2, 3: 4}).map((v, k) => [k*k, v*v*v]).toJS()
// { 1: 8, 9: 64 }
(I'm using ES6 via Babel.)
I realize that this isn't guaranteed to produce a defined result, due to the potential key collision. In my application I am preventing this elsewhere.
I'm actually using an OrderedMap
, if that makes a difference.
map()
function available facebook.github.io/immutable-js/docs/#/Map/map – Hinckley