1、將數組轉換成Set對象 2、數組去重 3、Set.prototype.has(value)判斷該值是否存在於Set對象中,返回布爾值 結合filter()方法,返回值為true的集合 ...
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'}]