


A brief discussion on the map data structure of es6 javascript_javascript skills
This article mainly introduces the map data structure of es6 javascript. The editor thinks it is quite good. Now I will share the JavaScript source code with you. Also as a reference for everyone. If you are interested in JavaScript, please follow the editor to take a look.
This article introduces the map data structure of es6 javascript and shares it with everyone. The details are as follows:
Map structure The purpose and basic usage
JavaScript object (Object) is essentially a collection of key-value pairs (Hash structure), but traditionally onlyString as a key. This brings great limitations to its use.
1 2 3 4 |
|
The original intention of the above code is to use a DOM node as the key of the object data, but since the object only accepts strings as key name, so element is automatically converted to a string [object HTMLpElement].
To solve this problem, ES6 provides the Map data structure. It is similar to an object and is also a collection of key-value pairs, but the scope of "key" is not limited to strings. Various types of values (including objects) can be used as keys. In other words, the Object structure provides the "string-value" correspondence, and the Map structure provides the "value-value" correspondence, which is a more complete implementation of the Hash structure. If you need a "key-value" data structure, Map is more suitable than Object.
1 2 3 4 5 6 7 8 9 |
|
The above code uses the set method to treat object o as a key of m, and then uses the get method to read Get this key, and then use the delete method to delete the key.
As a constructor , Map can also accept an array as a parameter. The members of this array are arrays representing key-value pairs.
1 2 3 4 5 6 7 8 9 |
|
The above code specifies two keys, name and title, when creating a new Map instance.
The Map constructor accepts an array as a parameter, and actually executes the following algorithm.
1 2 3 4 5 6 |
|
In the following example, the string true and the Boolean true are two different keys.
1 2 3 4 5 6 |
|
If the same key is assigned multiple times, the subsequent value will overwrite the previous value.
1 2 3 4 5 |
|
The above code assigns two consecutive values to key 1, and the latter value overwrites the previous value.
If an unknown key is read, undefined is returned.
1 2 |
|
Note that only references to the same object will be considered the same key by the Map structure. Be very careful about this.
1 2 3 |
|
The set and get methods of the above code appear to be for the same key, but in fact they are two The value and memory address are different, so the get method cannot read the key and returns undefined.
Similarly, two instances of the same value are regarded as two keys in the Map structure.
1 2 3 4 5 6 7 8 |
|
In the above code, the values of variables k1 and k2 are the same, but they are regarded as two in the Map structure key.
It can be seen from the above that the keys of Map are actually bound to the memory address. As long as the memory addresses are different, they are regarded as two keys. This solves the problem of collision of properties with the same name. When we extend other people's libraries, if we use objects as key names, we don't have to worry about our own properties having the same name as those of the original author.
If the key of the Map is a simple type of value (number, string, Boolean), then as long as the two values are strictly equal, the Map will treat it as a key, including 0 and - 0 . In addition, although NaN is not strictly equal to itself, Map treats them as the same key.
1 2 3 4 5 |
|
The above is the entire content of this article. I hope it will be helpful to everyone’s learning, and I also hope everyone will support PHP. Chinese website.
Related recommendations:
ie8 does not support the javascript map method
A brief analysis of the working principle of JavaScript mapreduce_Basic knowledge
The above is the detailed content of A brief discussion on the map data structure of es6 javascript_javascript skills. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

When using complex data structures in Java, Comparator is used to provide a flexible comparison mechanism. Specific steps include: defining the comparator class, rewriting the compare method to define the comparison logic. Create a comparator instance. Use the Collections.sort method, passing in the collection and comparator instances.

Data structures and algorithms are the basis of Java development. This article deeply explores the key data structures (such as arrays, linked lists, trees, etc.) and algorithms (such as sorting, search, graph algorithms, etc.) in Java. These structures are illustrated through practical examples, including using arrays to store scores, linked lists to manage shopping lists, stacks to implement recursion, queues to synchronize threads, and trees and hash tables for fast search and authentication. Understanding these concepts allows you to write efficient and maintainable Java code.

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Reference types are a special data type in the Go language. Their values do not directly store the data itself, but the address of the stored data. In the Go language, reference types include slices, maps, channels, and pointers. A deep understanding of reference types is crucial to understanding the memory management and data transfer methods of the Go language. This article will combine specific code examples to introduce the characteristics and usage of reference types in Go language. 1. Slices Slices are one of the most commonly used reference types in the Go language.

AVL tree is a balanced binary search tree that ensures fast and efficient data operations. To achieve balance, it performs left- and right-turn operations, adjusting subtrees that violate balance. AVL trees utilize height balancing to ensure that the height of the tree is always small relative to the number of nodes, thereby achieving logarithmic time complexity (O(logn)) search operations and maintaining the efficiency of the data structure even on large data sets.

Overview of Java Collection Framework The Java collection framework is an important part of the Java programming language. It provides a series of container class libraries that can store and manage data. These container class libraries have different data structures to meet the data storage and processing needs in different scenarios. The advantage of the collection framework is that it provides a unified interface, allowing developers to operate different container class libraries in the same way, thereby reducing the difficulty of development. Data structures of the Java collection framework The Java collection framework contains a variety of data structures, each of which has its own unique characteristics and applicable scenarios. The following are several common Java collection framework data structures: 1. List: List is an ordered collection that allows elements to be repeated. Li

Introduction to JS-Torch JS-Torch is a deep learning JavaScript library whose syntax is very similar to PyTorch. It contains a fully functional tensor object (can be used with tracked gradients), deep learning layers and functions, and an automatic differentiation engine. JS-Torch is suitable for deep learning research in JavaScript and provides many convenient tools and functions to accelerate deep learning development. Image PyTorch is an open source deep learning framework developed and maintained by Meta's research team. It provides a rich set of tools and libraries for building and training neural network models. PyTorch is designed to be simple, flexible and easy to use, and its dynamic computation graph features make
