[Immutable.js] Using Fromjs () to Convert Plain JavaScript Objects into immutable Data

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.