immutable.js - How to map over key-values in a map to produce another map with ImmutableJS? -
how, using immutablejs, produce new map mapping on key/value pairs of input map?
in scala, 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 key , cubes value.)
in javascript, i'm hoping 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 isn't guaranteed produce defined result, due potential key collision. in application preventing elsewhere.
i'm using orderedmap
, if makes difference.
the function looking mapentries
. here's example:
immutable.fromjs({1: 2, 3: 4}).mapentries(([k, v]) => [k*k, v*v*v]).tojs() // { 1: 8, 9: 64 }
mapentries
takes function takes key/value pair array, , should return new key/value pair array.
Comments
Post a Comment