What do you know about ES6 Map data structures?

Mondo Technology Updated on 2024-02-21

The map object holds key-value pairs and remembers the original insertion order of the keys. Any value (object or primitive type) can be a key or a value.

A map object is a collection of key-value pairs. A key in a map can only appear once; It is unique among the map's collections. map object key-value pair iteration - a for....The of loop returns an array of [key,value] after each iteration. Iterations are in insertion order, i.e., key-value pairs are iterated in the order in which the set() method was first inserted into the collection (that is, when set() is called, there are no keys with the same value in the map).

Static properties

size attribute: The size attribute returns the total number of members of the map structure. Instance method

set(key, value): The set method sets the key value and returns the entire map structure. If the key already has a value, the key value is updated, otherwise a new key is generated. get(key): The get method reads the key value corresponding to the key, and returns undefined if the key cannot be found. has(key): The has method returns a boolean value that indicates whether a key is in a map data structure. The delete(key):d elete method deletes a key, returning true. If the deletion fails, return false. clear(): The clear method clears all members with no return value. foreach(): Iterate through all members of the map. Iterative approach

keys(): A traverser that returns the key name. values(): A traverser that returns a key-value. entries(): A traverser that returns all members. map.prototype[@@iterator](): Returns a new iteration object, which is an array of [key, value] containing all key-value pairs in the map object, arranged in the order in which the map objects were inserted. map can be copied like an array:

const original = new map([ 1, 'one'],]const clone = new map(original);console.log(clone.get(1));// oneconsole.log(original === clone); // false.Shallow comparison is not a reference to the same object
Map objects can be merged with each other, but the keys remain unique.

const first = new map([ 1, 'one'], 2, 'two'], 3, 'three'],]const second = new map([ 1, 'uno'], 2, 'dos']]) When merging two map objects, if there are duplicate key values, the later ones will overwrite the earlier ones. The expansion syntax essentially converts a map object into an array. const merged = new map([.first, .second]);console.log(merged.get(1));// unoconsole.log(merged.get(2));// dosconsole.log(merged.get(3));// three
Map objects can also be combined with numbers:

const first = new map([ 1, 'one'], 2, 'two'], 3, 'three'],]const second = new map([ 1, 'uno'], 2, 'dos']]) When a map object is merged with an array, if there are duplicate key values, the later ones will overwrite the previous ones. const merged = new map([.first, .second, [1, 'eins']])console.log(merged.get(1));// einsconsole.log(merged.get(2));// dosconsole.log(merged.get(3));// three

Related Pages