1、将数组转换成Set对象
let arr1 = new Set([1,2,3,4]) console.log(arr1) //{1,2,3,4}
2、数组去重
let arr2 = new Set([1,1,2,3,4]) let arr3 = [...arr2] console.log(arr2, arr3) // {1,2,3,4} [1,2,3,4]
3、Set.prototype.has(value)判断该值是否存在于Set对象中,返回布尔值
let arr4 = new Set([1,2,3,4]) console.log(arr4.has(1)) // true
结合filter()方法,返回值为true的集合
let arr = [{id:1, name: 'aaa'}, {id: 2, name: 'bbb'}, {id:3, name: 'cccc'}, {id:4, name: 'dddd'}] let arr1 = [1,2] let newarr = arr.filter((item)=>{ return new Set(arr1).has(item.id) }) console.log(newarr) // [{id:1, name: 'aaa'}, {id:2, name: 'bbb'}]