structure
const set = new Set([1, 2, 3, 4, 4]);
Acceptable parameters for all data interface having iterable
characteristic:
Similarly array, no duplicate values.
const set = new Set([1, 2, 3, 4, 4]); [...set] //1,2,3,4
important point:
1. Members between duplicate values similar to values determined === ', but a plurality would be judged to be equal NaN
const set = new Set([1,NaN, NaN, 4, '4']); console.log(...set)
2. The two objects are not equal to the total
const set = new Set([{}, {},{name:'shyno'},{name:'shyno'}]); console.log(...set)
3. No key value only
const arr = [1,2,2,3,4,5,5,6] let obj = {name:'shyno'} const set = new Set(arr); const setArr = Array.from(set) console.log(Object.keys(arr),Object.keys(obj),Object.keys(set))
(It can be seen key set is empty)
4.Set not an array
const set = new Set([{}, {},{name:'shyno'},{name:'shyno'}]); document.write(set[0]) //undefined console.log(set.concat([1,2])) //set.concat is not a function
(. 1) Set the array can be used to de-emphasis, however, the need to sub-arrays Set
const set = new Set([1,2,3,3,4,4,5]); const arr = Array.from(set) console.log(arr[0],arr.concat(['1','2']))
(2) Set may also be used to re-string
const set = new Set('aabbccdee'); const arr = Array.from(set) console.log(arr[0],arr.concat(['1','2']))
(3) Other data interface having iterable duplicate values which can be removed by this method
Properties and Methods
Constructors: Set.prototype.constructor
console.log(Set.prototype.constructor)
Total length / size / members: Set.prototype.size
ARR = const [1,2,2,3,4,5,5,6] const new new SET = the Set (ARR); const = setArr Array.from (SET) the console.log ( 'original array length', arr. length, 'the total number of members of the set of', set.size, 'set length after transfer array', setArr.length)
Add members: add () ------ add some value, return Set structure itself
const arr = [1,2,2,3,4,5,5,6] const set = new Set(arr); set.add(2) const setArr = Array.from(set) console.log(set,set.add('shyno'))
(Note that the same print out the results, indicating its return value is the set itself)
Without repeated, different types of data may be added
To remove a member: delete () ------ delete a value, it returns a Boolean value that indicates whether the deleted successfully
const arr = [1,2,2,3,4,5,5,6] const set = new Set(arr); const setArr = Array.from(set) console.log(set) console.log(set .delete(2)) console.log(set) console.log(set .delete(2))
(Set no repeat value, so the same operation, only the first will succeed)
No presence determination: has (value) ----- returns a Boolean value indicating whether the value is Set
a member of
const arr = [1,2,2,3,4,5,5,6] const set = new Set(arr); const setArr = Array.from(set) console.log(set) console.log(set .has(2)) console.log(set .delete(2)) console.log(set .has(2)) console.log(set)
(Here, obviously when there is no print set 2, but has (2) is indeed true, the delete operation was false)
Empty members: clear () ----- remove all members, no return value
const arr = [1,2,2,3,4,5,5,6] const set = new Set(arr); const setArr = Array.from(set) console.log(set) console.log(set .clear()) console.log(set .delete(2)) console.log(set.add(2)) console.log(set .has(2)) console.log(set)