JavaScript 标准库Set方法实例(3)
1.Set方法用法 实例
(1).用法1 数组去重
let arr = ['1','1','2','2','43','32','54','76'];
let brr = ['1','20','4','32','54','23'];
function objSet(arr,brr=[]) {
// 1.传值arr brr 表示清除2个数组的值
// 2.传一个数组 表示去除一个的值
let newArr = arr.concat(brr);
let set = new Set(newArr);
let newArry = Array.from(set);
return newArry;
}
console.log(objSet(arr,brr));
(2). 数组去重
let arr = ['1','1','2','2','43','32','54','76'];
let brr = ['1','20','4','32','54','23'];
function objSet(arr,brr=[]) {
// 1.传值arr brr 表示清除2个数组的值
// 2.传一个数组 表示去除一个的值
let newArr = arr.concat(brr);
let set = new Set(newArr);
let newArry = [...new Set(set)];
return newArry;
}
console.log(objSet(arr,brr));
(3). 数组去重
let set1 = new Set([1, 2, 3, 4, 5, 6]);
let set2 = new Set([4, 5, 6, 7, 8, 9]);
let set3 = new Set([4, 5, 9, 17, 18, 19]);
let union = new Set([...set1, ...set2, ...set3]);
// let myArr = Array.from(union);//1.转化为数组
var myArr = [...union];//2.转化为数组