导航
var set = new Set();
console.log(set);
var set = new Set();
set.add(5);
set.add(7);
console.log(set);
var set = new Set([5, 7]); // 参数只能是具备 iterator 接口的数据结构
var set1 = new Set([1,2,3,5,5,5]);
console.log(set1);
var set = new Set([
undefined, undefined,
null, null,
NaN, NaN,
5, '5',
{}, {},
[], [],
function() {}, function() {}
]);
console.log(set);
NaN !== NaN
,但 Set 还是会对 NaN 去重const set = new Set([1]);
console.log(set.add(2));
const set = new Set();
set.add(1).add(2);
console.log(set);
var x = {id: 1},
y = {id: 2};
var set = new Set();
set.add(x);
set.add(y);
set.add(x); // set进去的是对象的引用,所以只会set进去一个x
console.log(set);
var set = new Set([{a:1}])
console.log(set);
console.log(set.size); // 1
var x = {id: 1},
y = {id: 2};
var set = new Set();
set.add(x).add(y);
console.log(set.delete(x));
console.log(set.delete(x));
console.log(set);
const set = new Set([1,2,3]);
console.log(set.clear());
console.log(set);
var x = {id: 1},
y = {id: 2};
var set = new Set();
set.add(x).add(y);
console.log(set.has(x));
var set = new Set([1,2,3,4,5]);
console.log(set.keys());
console.log(set.values());
console.log(set.entries());
var set = new Set(['a','b','c','d']);
for(let i of set.keys()){
console.log('key-', i);
}
for(let i of set.values()){
console.log('value-', i);
}
var set = new Set(['a','b','c','d']);
for(let i of set.entries()){
console.log('entries-', i);
}
var set = new Set(['a','b','c','d']);
for(let i of set){
console.log(i);
}
console.log(
Set.prototype[Symbol.iterator] === Set.prototype.values);
var set = new Set(['a','b','c','d']);
set.forEach(function(value, key, set){
console.log(value, key, set);
})
var set = new Set(['a','b','c','d', 'a']);
console.log([...set]);
var set = new Set([1,2,3,4,5]);
var set1 = new Set([...set].map(v => v * 2));
console.log(set1);
var arr = [1,2,3,4];
var arr2 = arr.map((v, k) => console.log(v, k));
var arr1 = arr.map(parseInt);
console.log(arr1);
// 相当于执行 parseInt(1, 0)、parseInt(2, 1) ...
console.log(parseInt(1, 0)); // 第二个参数是进制
console.log(parseInt(2, 1));
console.log(parseInt(3, 2));
console.log(parseInt(4, 3));
var set = new Set([1,2,3,4]);
var set2 = new Set([[...set].map(parseInt)]);
console.log(set2);
var set = new Set([1,2,3]);
var set1 = new Set(Array.from(set, value => value * 2));
console.log(set1);
var set = new Set([1,2,3,4]);
var set2 = new Set([[...set].filter(x => x % 2 == 0)]);
console.log(set2);
var a = new Set([1,2,3]);
var b = new Set([4,3,2]);
var union = new Set([...a, ...b]);
console.log('并集', union);
var intersect = new Set([...a].filter(x => b.has(x)));
console.log('交集', intersect);
var difference = new Set([...a].filter(x => !b.has(x)));
console.log('差集', difference);