Immutable.js has one more tool for creating immutable data in addition to everything we've looked at so far in this chapter. The fromJS()
function is similar to a JSON parser, because it parses the same types of JavaScript objects that you'll see when you parse JSON strings: objects and arrays.
You can parse a regular JavaScript array, resulting in a list, as shown here:
import { fromJS } from 'immutable'; const myList = fromJS([1, 2, 3]); console.log('myList', myList.get(0)); // -> myList 1
This is just like passing the array to the List
constructor.
You can parse a regular JavaScript object, resulting in a map:
const myMap = fromJS({ one: 1, two: 2, three: 3 }); console.log('myMap', myMap.get('one')); // -> myMap 1
Once again, this is just like passing the object literal to the Map
constructor.