Immutable.js offers the Fromjs () method to build immutable structures from objects and array. Objects is converted into maps. Arrays is converted into lists. The Fromjs () method can also take a reviver function for custom conversions.
Object to immutable Map:
check by IsMap ():
Const Plainjsobject = { "Go to Grocery", "I need milk and eggs", false" c8>, "house duties", Priority:10} }; = Immutable.fromjs (plainjsobject); Expect (Immutable.Map.isMap (Immutabletodo)). to.be. true
We cat Get Value by Getin () method:
Expect (Immtabletodo.getin (["category", "title"]). To.equal ("house duties");
Array to immutable List:
Check by Islist ():
Const Plainjsarray = [ "Go to Grocery", "Buy milk and Eggs", "help kids with Homew Ork ", [" Buy Lemons "," Make Lemonade "] ]; = Immutable.fromjs (plainjsarray); Expect (Immutable.List.isList (immutabletodolist)). to.be. true;
Get value by Getin ():
Expect (Immutabletodolist.getin ([3, 1]). To.equal ("Make Lemonade")
Convert a plain array to immutable MAP:
Const Plainjsarray = [ "Go to Grocery", "Buy milk and Eggs", "help kids with Homew Ork ", [" Buy Lemons "," Make Lemonade "] ]; = Immutable.formjs (Plainjsarray, (key, value) =>{ return value.tomap (); }); Expect (Immutabletodolist.getin ([3,1])). To.equal ("Make lemonade");
[Immutable.js] Using Fromjs () to Convert Plain JavaScript Objects into immutable Data