<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Set(交集、并集、差集)</title> </head> <body> </body> </html> <script type="text/javascript"> var set1=new Set([1,2,1,2,1,2,true,NaN,true,"1","1"]); //得到一个去重后的类数组(类似数组的对象) //只有值value没有键key的对象 console.log(set1); //size 个数 //add console.log(set1.add(10)); //has console.log(set1.has(1)); //delete true/false console.log(set1.delete(1));;js知识点 console.log(set1); //clear 没有返回值 set1.clear(); console.log(set1); var ary=[1,2,1,2,1,2,true,NaN,true,"1","1"]; console.log([...new Set(ary)]); var ary1=[1,2,4,6,8]; var ary2=[1,3,5,6,7,8]; //并集 function bingJi(ary1,ary2) { return [...new Set([...ary1,...ary2])].sort(); } console.log(bingJi(ary1, ary2)); //交集 function jiaoJi(ary1,ary2) { return ary1.filter(function (item) { return ary2.includes(item) }) } console.log(jiaoJi(ary1, ary2)); //差集(并集-交集) function chaJi(ary1,ary2) { return bingJi(ary1,ary2).filter(function (item) { return !jiaoJi(ary1,ary2).includes(item); }) } console.log(chaJi(ary1, ary2)); var map=new Map([["a","a"],["b","b"]]); console.log(map); </script>
Set(交集、并集、差集)
最新推荐文章于 2022-12-12 11:42:26 发布