Immutable.js provides several conversion methods to migrate one structure to another. Each Immutable.js class contains a prefixed ' to ' method like Map.tolist (), Map.toset (), etc. Converting these types sometimes results in a loss of data, as we'll see if converting from the Map to List.
Map to List:
It (' should convert Map () to List () ', () = = Immutable.map ({ ' first Item ', ' Second Item ' }); = map.tolist (); Expect (Immutable.List.isList (convertedlist)). to.be. true ; // Keys are discarded Expect (Convertedlist.first ()). To.equal (' first Item '); Expect (Convertedlist.last ()). To.equal (' Second Item '); });
List to Map:
It (' should convert List () to Map () ', () = = Immutable.List.of (' First item ', ' Second item '); = list.tomap (); // converted Keys Ascend numerically Keys = Convertedmap.keys (); Expect (Keys.next (). Value). to.equal (0); Expect (Keys.next (). Value). to.equal (1); Expect (Immutable.Map.isMap (Convertedmap)). to.be. true ; Expect (Convertedmap.first ()). To.equal (' first Item '); Expect (Convertedmap.last ()). To.equal (' Second Item '); });
Map to Javascript Array:
It (' should convert Map () to Javascript Array ', () = = Immutable.map ({ ' first Item ', c6/>' Second item ', ' Nested item '} }); = Map.toarray (); // Keys are discarded Expect (Arr[0]). To.equal (' first Item '); Expect (arr[1]). To.equal (' Second Item '); Expect (arr[2].key4). To.equal (' Nested Item '); });
Map to JSON:
It (' should convert Map () to JSON ', () = = Immutable.map ({ ' first Item ', ' Second item ', ' Nested item '} }); = Map.tojson (); Expect (json.key1). To.equal (' first Item '); Expect (Json.key2). To.equal (' Second Item '); Expect (Json.key3.key4). To.equal (' Nested Item '); });
[immutable.js] Converting immutable.js structures to Javascript and other immutable Types