set结构跟数组类似,但set没有重复值
数组去除成员重复的方法
let array = [1, 2, 3, 4, 4]
[...new Set(array)]
const items = new Set([1, 2, 3, 4, 5, 5]);
const array = Array.from(items);
去除字符串里面的重复字符
[...new Set('ababbc')].join('')
数组并集[1,2,3,4]
let a = new Set([1, 2, 3]);
let b = new Set([4, 3, 2]);
[...new Set([...a, ...b])]
数组交集输出[2,3]
let a = new Set([1, 2, 3]);
let b = new Set([4, 3, 2]);
[...new Set([...a].filter(x => b.has(x)))]
数组差集[1]
let a = new Set([1, 2, 3]);
let b = new Set([4, 3, 2]);
[...new Set([...a].filter(x => !b.has(x)))]
set方法有add,delete,has(判断值是否存在,返回布尔值),clear(清空),keys,values,forEach
keys遍历键名,values遍历键值,但是因为Set没有键名,所以keys和values得到的结果是一样的
set变量size