NestedObjectMap Class
Deep convert a nested Object into a ES6 Map
npm install nested-object-map
Usage
new NestedObjectMap([object])
The NestedObjectMap Class extends Map and behaves the same way with the difference that the passed objects field values will be mapped. If the object is nested (containing more instances of Object) references to those objects will be mapped as well. Sub-objects will be iterated and their fields will be mapped just the same but their field names will be prefixed with its path. Cyclic references and other objects (eg. arrays) will be mapped as reference. Scalar values are mapped as scalar values, setting them on the map will not change the original objects value.
const NestedObjectMap = ; const config = api: http: auth: token: 'secret' ; const authToken = config;const token = config; consoledirauthToken; // "secret"consoledirtoken; // "secret"
NestedObjectMap.mapObject([object])
You can map another objects fields to the Map (similar to a "deep merge"). This is the same method the constructor calls.
config; consoledirconfig; // 3000consoledirconfig; // "secret"
Use case
It can help you to access values contained deep within nested objects more easily and without the need of too many safe guards. It's probably most useful when dealing with nested objects with unreliable structure.
if object && objectapi && objectapihttp && objectapihttpauth const token = objectapihttpauthtoken; // VS const config = object;const token = config;
Related
There are similar modules for flat objects: