Deeply converts plain JS objects and arrays to Immutable Maps and Lists.
fromJS(json: any, reviver?: (k: any, v: Iterable<any, any>) => any): any
If a reviver
is optionally provided, it will be called with every
collection as a Seq (beginning with the most nested collections
and proceeding to the top-level collection itself), along with the key
refering to each collection and the parent JS object provided as this
.
For the top level, object, the key will be ""
. This reviver
is expected
to return a new Immutable Iterable, allowing for custom conversions from
deep JS objects.
This example converts JSON to List and OrderedMap:
Immutable.fromJS({a: {b: [10, 20, 30]}, c: 40}, function (key, value) {
var isIndexed = Immutable.Iterable.isIndexed(value);
return isIndexed ? value.toList() : value.toOrderedMap();
});
// true, "b", {b: [10, 20, 30]}
// false, "a", {a: {b: [10, 20, 30]}, c: 40}
// false, "", {"": {a: {b: [10, 20, 30]}, c: 40}}
If reviver
is not provided, the default behavior will convert Arrays into
Lists and Objects into Maps.
reviver
acts similarly to the same parameter in JSON.parse
.
Immutable.fromJS
is conservative in its conversion. It will only convert
arrays which pass Array.isArray
to Lists, and only raw objects (no custom
prototype) to Map.
Keep in mind, when using JS objects to construct Immutable Maps, that JavaScript Object properties are always strings, even if written in a quote-less shorthand, while Immutable Maps accept keys of any type.
var obj = { 1: "one" };
Object.keys(obj); // [ "1" ]
obj["1"]; // "one"
obj[1]; // "one"
var map = Map(obj);
map.get("1"); // "one"
map.get(1); // undefined
Property access for JavaScript Objects first converts the key to a string,
but since Immutable Map keys can be of any type the argument to get()
is
not altered.