Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用。
new Set([,,,])初始化var arr = [1,7,2,1,7,9,20,13,9]; var s = new Set(arr); //{1, 7, 2, 9, 20, 13} s.add(100); alert(s.has(100)) // true var arr2 = Array.from(s); // 类数组set转 数组 console.log(arr2); [1, 7, 2, 9, 20, 13, 100]数组去重
var mySet = new Set([1, 2, 3, 4, 4]); [...mySet]; // [1, 2, 3, 4]并集
var a = new Set([1, 2, 3]); var b = new Set([4, 3, 2]); var union = new Set([...a, ...b]); // {1, 2, 3, 4}