How do I convert a C3 map to plain object in JavaScript?

I have a C3 map:

var map = c3Make('map<string, string>', { a: 'A' });

and I would like to convert it to a plain JS object, but I don’t see any .toJSON / .toJson method available, which is available for all Obj. I tried the .toObject method, but that just returns a new C3 map, not a plain JS object.

The only workaround I can think of is:

JSON.parse(JSON.stringify(map))

but I would like to avoid that for obvious reasons.

Are there any other workarounds or features I’m missing?

1 Like

After investigating further and looking into how JSON.stringify works internally (it iterates over the enumerable properties and calls .toJSON if defined on each value, plus other logic), I’ve found that Object.assign can work, but only for the first level:

Object.assign({}, map);

But it doesn’t work if you have a map whose value type is another data type that doesn’t implement toJSON, such as another map…

var map = c3Make('map<string, map<string, string>>', { a: { b: 'AB' } });

so I’m really interested in a generic solution. Any help would be appreciated!