First, create an object instance Set
Set object allows you to store a unique value of any type, whether it is the original value or object reference
1. Constructor
Syntax: new Set ([iterable])
Parameters:
Iterable if an iteration pass objects, all of its elements will be added to the new Set; if this parameter is not specified or is null, then a new empty Set
let arr = [1,2,2,3];
let mySet = new Set(arr);
console.log(mySet); // Set(3) {1, 2, 3}
Two, Set instance properties
1.size property will return the number of object elements Set
let mySet = new Set();
mySet.add(1);
mySet.add(5);
mySet.add("some text");
console.log(mySet.size); // 3
Three, Set instance method
1.Add () method is used to add a specified value to the end of a Set of Objects
Syntax: mySet.add (value)
parameter:
Value value required, you need to add to Set object elements
let mySet = new Set();
mySet.add(1);
mySet.add(5).add("some text"); // 可以链式调用
console.log(mySet); // Set [1, 5, "some text"]
2.delete () method to remove the specified element from a Set object
Syntax: mySet.delete (value)
parameter:
The value of the element to be deleted
Return Value: deleted successfully return true, false otherwise
let mySet = new Set();
mySet.add("foo");
mySet.delete("foo"); // 返回 true,删除成功
console.log(mySet.size); // 0
3.Clear () method is used to clear all the elements of a subject Set
Syntax: mySet.clear ()
let mySet = new Set();
mySet.add(1);
mySet.add("foo");
console.log(mySet.size); // 2
mySet.clear();
console.log(mySet.size); // 0
4.has () method returns a boolean value to indicate whether there is a value corresponding to the object Set
Syntax: mySet.has (value)
parameter:
value 必须,是否存在于Set的值
Returns: If the specified value (value) Set in the presence of an object which returns true; otherwise false
let mySet = new Set();
mySet.add("foo");
console.log(mySet.has("foo")); // true
console.log(mySet.has("bar")); // false
5.entries()
Syntax: mySet.entries ()
Return Value: a new sequence comprising [value, value] in the form of an array of iterator object, value is given for each element of the set, the order of elements iterator object collection object, i.e. each element is inserted
let mySet = new Set();
mySet.add("foobar");
mySet.add(1);
mySet.add("baz");
let setIter = mySet.entries();
console.log(setIter.next().value); // ["foobar", "foobar"]
console.log(setIter.next().value); // [1, 1]
console.log(setIter.next().value); // ["baz", "baz"]
6.values()
Syntax: mySet.values () or mySet.keys ()
Return Value: returns an Iterator object that the object to insert Set Set original target sequence contains in each element
let mySet = new Set();
mySet.add("foo");
mySet.add("bar");
mySet.add("baz");
let setIter = mySet.values();
console.log(setIter.next().value); // "foo"
console.log(setIter.next().value); // "bar"
console.log(setIter.next().value); // "baz"
7.forEach()
Syntax: mySet.forEach (callback [, thisArg])
Parameters:
function callback will be executed for each element
thisArg when executing callback functions, it can be used as this
let mySet = new Set(["foo","bar",undefined]);
mySet.forEach((value1,value2,set) => {
console.log("key =",value1,",value =",value2); // key = foo ,value = foo
});