<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Set实现数组去重、交集、并集、差集</title> </head> <body> </body> <script> let {log} = console; let arr = [1,2,3,3]; let arr1 = [1,2,3,3,5]; // 去重 let set1 = new Set([...arr]); log([...set1]);// [1,2,3] // 并集 let set2 = new Set([...arr, ...arr1]); log([...set2]);// [1,2,3,5] // 交集 let set3 = [...set1].filter(x=>new Set([...arr1]).has(x)); log([...set3]);// [1,2,3] // 差集 let set4 = new Set([...set2].filter(function (x) { return !set1.has(x) })) log([...set4]); // [5] let setarr1 = new Set([1,2]); setarr1.add(0); log(setarr1); setarr1.delete(2); log(setarr1); </script> </html>