ES6-Mapify
Convert JS Objects to ES6 Maps and vice versa.
ES6 Map
objects are really nice for iteration, but they're not so nice for
directly referencing properties, the way JS Objects are. This is a nice way to
convert back and forth. First, simply use npm
to include es6-mapify
in your
project's dependencies:
npm install -S es6-mapify
Now you can import it and use it like so:
; // converts basic objectslet myObj = foo: 'bar';let myMap = ;myMap; // 'bar' // doesn't do anything to non-objects; // 'foo';; // null // is smart about objects nested inside arrays and other objectslet arrMap = ;arrMap2; // 3arrMap1; // 'bar' let myMap = ;myMap; // 'baz';
Of course, you might want to go the other direction too! If you have a Map
and want the corresponding basic JS object, just use demapify
:
; // converts basic mapslet myMap = ;myMap;; // {foo: 'bar'} // doesn't do anything to non-objects; // 2 // is smart about nested Maps (and Maps in arrays)let myMap = myMap2 = ;myMap;myMap2;myMap;; // {foo: 'bar', inception: {baz: 'quux'}}