let arr = [1,2,3,'5','5']; let st = new Set(arr); console.log(st); // 可以通过set来去除数组的重复的值,返回的是一个伪数组 console.log(st.size); // 4 |
let st = new Set(); var u = {name:'Joh'}; st.add(u); let bool = st.delete(u); console.log(bool); // true; |
let st = new Set(); var u = {name:'Joh'}; var r = {name:'Lily'}; st.add(u).add(r); let bool = st.delete(r); console.log(bool); // true console.log(st.has(r)); // false console.log(st.has(u)); // true; |
let st = new Set(); var u = {name:'Joh'}; var r = {name:'Lily'}; st.add(u).add(r); st.clear(); console.log(st.size); // 0 |
let arr = ['xxx', 'yyyy', 'yyyy']; let newArr = Array.from(new Set(arr)); console.log(Array.isArray(newArr)); // true console.log(newArr); // ["xxx", "yyyy"] |
console.log(Set.prototype[Symbol.iterator] === Set.prototype.values); // true let st = new Set(['xxx', 'yyyy', 'yyyy', 'John']); for(let k of st) { console.log(k); // 依次输出 xxx yyyy John 可以直接遍历,兼容map的数据结构 } |
let st = new Set(['xxx', 'yyyy', 'yyyy', 'John']); console.log(st.size); // 3 let itKeys = st.keys(); for(let k of itKeys) { console.log(k); // 依次输出 xxx yyyy John } console.log('>>>>>'); let itVals = st.values(); for(let v of itVals) { console.log(v); // 依次输出 xxx yyyy John } |
let st = new Set(['xxx', 'yyyy', 'yyyy', 'John']); let entriesIt = st.entries(); // for(let v of entriesIt) { console.log(v); // 依次输出 ["xxx", "xxx"] ["yyyy", "yyyy"] ["John", "John"] } |
let st = new Set(); console.log(NaN === NaN); // false , 此处 NaN 是不全等的,理应可以添加多个,不算重复,但是这里是个特例 st.add(NaN).add(NaN).add(NaN); for(let v of st) { console.log(v); // 只输出一个 NaN } |
|