Set
Object allows you to store a unique value of any type, whether it is an original value or an object reference.
1 Const SET1 = new Set ([1, 2, 3, 4, 5]); 2 3 Console.log (Set1.has (1)); 4//Expected Output:true 5 6 Cons Ole.log (Set1.has (5)); 7//Expected Output:true 8 9 Console.log (Set1.has (6));//Expected output:false
Grammar
New Set ([iterable]);
Parameters
-
Iterable
-
If you pass an iterator object, all its elements are added to the new
Set . If you do not specify this parameter or its value is null, the new
Set is empty.
return value
A new Set
object.
Briefly
Set
An object is a collection of values, and you can iterate over its elements in the order in which they are inserted. The elements in the set appear only once, that is, the elements in the set are unique.
equality of Values
Because the values in the Set are always unique, you need to determine whether the two values are equal. In earlier versions of the ECMAScript specification, this was not the same algorithm based on the algorithm used in the = = = operator. Specifically, for Set s, +0 (+0 strictly equals-0) and-0 are different values. However, this has been changed in the ECMAScript 2015 specification. For more information, see "Value equality for-0 and 0" in the browser compatibility table.
In addition, NaN
and undefined
both can be stored in the set, and are NaN
treated as the same value (although Nan!== nan).
Property
-
Set.length
-
length
The value of the property is 0.
-
get Set[@@species]
-
constructor is used to create a derived object.
-
Set.prototype
-
represents the prototype of the set constructor, allowing new properties to be added to all set objects.
Set
Instance
All set instances inherit from Set.prototype
.
Properties
-
Set.prototype.constructor
-
Returns the constructor for the instance. By default
Set
.
-
Set.prototype.size
-
returns the
Set
number of values for the object.
Method
-
-
Set.prototype.add(value)
-
-
in
Set对象尾部添加一个元素。返回该
Set对象。
the
-
-
Set.prototype.clear()
-
-
removes
Set
all elements within an object.
-
-
Set.prototype.delete(value)
-
-
移除Set的中与这个值相等的元素,返回Set.prototype.has(value)在这个操作前会返回的值(即如果该元素存在,返回true,否则返回false)。
Set.prototype.has(value)在此后会返回false。
-
-
Set.prototype.entries()
-
The
-
返回一个新的迭代器对象,该对象包含Set对象中的
所有元素的值的[value, value]数组。为了使这个方法
Map对象保持相似,
keys and values for each value are equal in order of insertion.
-
-
Set.prototype.forEach(callbackFn[, thisArg])
-
-
invokes a CALLBACKFN for each value in the Set object in the order in which they are inserted. If you provide
thisArg参数,回调中的this会是这个参数。
a
-
-
Set.prototype.has(value)
-
-
returns a Boolean value that represents the value in
Set中存在与否。
the
-
-
Set.prototype.keys()
-
-
in the
方法相同,返回一个新的迭代器对象,该对象包含Set对象中的
order of
所有元素的值。
insertion
values()
-
-
Set.prototype.values()
-
-
返回一个新的迭代器对象,该对象包含Set对象中的
in
所有元素的值。
Insert order
-
-
Set.prototype[@@iterator]()
-
-
返回一个新的迭代器对象,该对象包含Set对象中的
in
所有元素的值。
Insert order
Exampleworking with
Set
objects
1 Let MySet = new Set (); 2 3 Myset.add (1);//Set (1) {1} 4 Myset.add (5);//Set (2) {1, 5} 5 Myset.add (5);//set { 1, 5} 6 Myset.add ("some text"); Set (3) {1, 5, "some text"} 7 var o = {a:1, b:2myset.add (o); 9 Myset.add ({a:1, b:2});//O point to a different object, so no problem 11 Myset.has (1); True13 Myset.has (3); False14 Myset.has (5); True15 Myset.has (math.sqrt); True16 Myset.has ("Some Text". toLowerCase ()); True17 Myset.has (o); True18 myset.size; 520 Myset.delete (5); True to Remove 522 Myset.has (5) from set; False, 5 has been removed from myset.size; 4, just removed a value of Console.log (MySet); Set {1, "some text", object {a:1, b:2}, Object {a:1, b:2}}
Iterate set
1//Iteration Entire set 2//sequentially output: 1, "some text" 3 for(Let item of MySet) Console.log (item); 4 5//sequentially output: 1, "some text" 6 for(Let item of Myset.keys ()) Console.log (item); 7 8//sequentially output: 1, "some text" 9 for (Let item of Myset.values ()) Console.log (item); 10 11//Sequentially output: 1, "some text" 12//(Key and value equal) for (let [Key, VA Lue] of Myset.entries ()) Console.log (key); 14 15//Conversion set is an array (with array comprehensions)-var Myarr = [V for (V of Myse T)]; [1, "some text"]17//Alternative scheme (with Array.from) var Myarr = Array.from (MySet); [1, "some text"]19 20//If you work in an HTML document, you can also: myset.add (document.body); Myset.has (Document.queryselector (" Body ")); TRUE23//Set and array interchange MySet2 = new set ([1,2,3,4 ]); myset2.size;//427 [... mySet2];//[1,2,3,4]28 29// Intersect can be simulated via var intersection = new Set ([... set1].filter (x => set2.has (x))); +//differ Ence can be simulated via33 var difference = new Set ([...] set1].filter (x =! ) Set2.has (x))); 34 35//with a forEach iteration of Myset.foreach (function (value) {PNS console.log (value); + }); 39 40 141//242//343//4
Array
Related
1 var myArray = ["Value1", "value2", "Value3"];2 3//Use the Set constructor to convert the array to Set4 var myset = new Set (MyArray), 5 6 MySet. Has ("value1"); Returns TRUE7 8//With ... The (expand operator) operator converts the set to Array9 console.log ([... myset]); Fully consistent with MyArray
String
Related
1 var text = ' Indiana '; 2 3 var myset = new Set (text); Set {' I ', ' n ', ' d ', ' I ', ' a '}4 myset.size; 5
JavaScript---ES6 SET collection structure