Maps and JSON and ES6 Kits?

ES6 / Harmony Introduces New Data Types for Maps and Sets. Is it necessary in any case to load JSON into these types instead of the default Object and Array?

+4
source share
2 answers

Set does not have the same meaning as Array. A collection is a collection of unique elements, and an array is an ordered collection of elements that are not necessarily unique.

To load JSON into a map instead of an associative array, see JSON.parse . You can write a custom resolver, for example:

JSON.parse(json, (k, v) => {
        if(typeof v == 'object' && !(v instanceof Array))
            return new Map(Object.entries(v));
        return v;
    });

Map , Object.entries. Object.entries - , . :

Object.entries = function* entries(obj) {
   for (let key of Object.keys(obj)) {
     yield [key, obj[key]];
   }
}

( Object.prototype.entries, .)

+3

, " JSON ". , ?

. JSON ES6, API JSON. , , , JSON.parse .

0

Source: https://habr.com/ru/post/1529521/


All Articles