I created a custom fromJS function so that when I convert objects over to Immutable objects then the result would only contain OrderedSets and Maps (instead of Lists and Maps).
This has worked perfectly fine except for in the following case where a key in the object is called "length". Any ideas how to fix the issue at hand?
var imm = require("immutable")
function fromJS(js) {
return typeof js !== 'object' || js === null ? js :
Array.isArray(js) ?
imm.Seq(js).map(fromJS).toOrderedSet() :
imm.Seq(js).map(fromJS).toMap();
}
var output = fromJS({
measurements: {
length: 10,
weight: 30
}
}).toJS();
// output is :
{
measurements: {
0: undefined,
1: undefined
....
32: undefined
}
}
Got worked by changing the property
lengthtomLength.Here is the JSBin
ImmutableJS looks for
lengthproperty to check whether the given value is array-like object or not.Since your
measurementsobject has a propertylength, it considers that this object is anarray-likeobject and continues theSeqconstruction based onarray-liketype, here the problem starts.To confirm this, here is the jsbin which outputs the
Mapwith no.of time given in the length property of themeasurementsobject.