Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between es6's set and map

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you what are the relevant knowledge points about the difference between es6's set and map, the content is detailed, and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Differences: 1, set refers to the "collection" structure, while Map refers to the "dictionary" structure; 2, set is stored in the form of "[value, value]", while Map is stored in the form of "[key, value]"; 3, Map can use get () to find a specific value through the key and return, but set is not.

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

Brief description:

The main application scenarios of Set and Map are data reorganization and data storage.

Set is a data structure called a collection, and Map is a data structure called a dictionary.

The difference between a collection and a dictionary:

What they have in common: collections and dictionaries can store values that are not duplicated

Difference: collections store elements in the form of [value, value], and dictionaries store elements in [key, value].

Collection (Set):

A new data structure added to ES6 is similar to an array, but the members are unique and unordered, with no duplicate values.

Set itself is a constructor used to generate Set data structures.

The Set object allows you to store a unique value of any type, whether it's the original value or the object reference.

Const s = new Set () [1,2,3,4,3,2,1] .forEach (x = > s.add (x)) for (let i of s) {console.log (I) / / 1234} / / repeating objects of the de-duplicated array let arr = [1,2,3,2,1,1] [. New Set (arr)] / / [1,2,3]

Note: no type conversion occurs when adding values to Set, so `5` and `5 "`are two different values. Set uses an algorithm called "Same-value-zero equality" to determine whether the two values are different, which is similar to the * * exact equality * * operator (`=`). The main difference is that * * `NaN` equals itself, while the exact equality operator thinks that `NaN` is not equal to itself. **

Let set = new Set (); let a = NaN;let b = NaN;set.add (a); set.add (b); set / / Set {NaN} let set1 = new Set () set1.add (5) set1.add ('5') console.log ([... set1]) / / [5, "5"]

Method of operation:

Add (value): added, equivalent to push in array.

Delete (value): value is deleted from the collection when it exists.

Has (value): determines whether a value exists in the collection.

Clear (): clears the collection.

Traversal method: traversal method (traversal order is insertion order)

Keys (): returns an iterator that contains all the keys in the collection.

Values (): returns an iterator that contains all the worthy iterators in the collection.

Entries (): returns a key-value pair iterator that contains all the elements in the Set object.

ForEach (callbackFn, thisArg): used to perform callbackFn operations on collection members. If the thisArg parameter is provided, the this in the callback will be this parameter and no return value will be returned.

Dictionary (Map):

Is the structure of a set of key-value pairs, with extremely fast search speed.

Const m = new Map () const o = {p: ''} m.set (o,' content') m.get (o) / / contentm.has (o) / / truem.delete (o) / / truem.has (o) / / false

Method of operation:

Set (key, value): adds a new element to the dictionary.

Get (key): use the key to find a specific value and return it.

Has (key): determines whether the key key exists in the dictionary.

Delete (key): removes the corresponding data from the dictionary by using the key key.

Clear (): delete all elements in this dictionary.

Traversal method:

Keys (): returns all key names contained in the dictionary as an iterator.

Values (): returns all values contained in the dictionary as iterators.

Entries (): returns an iterator for all members.

ForEach (): traverses all the members of the dictionary.

These are all the contents of this article entitled "what's the difference between es6's set and map?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report