es6有那些方法可以判断两个数组是否有相同项

2025年03月08日 建站教程

es6有那些方法可以判断两个数组是否有相同项,下面web建站小编给打家介绍一下利用Set和filter函数的实现方法!

let arr1=['a', 'b', 'c'];
let arr2=['b', 'c', 'd'];
newA = new Set(arr1);
newB = new Set(arr2); 
let results = new Set([...newA].filter(x => newB.has(x)));
console.log('输出结果:',results);
//输出结果: Set(2) {'b', 'c'}

本文链接:http://so.lmcjl.com/news/24541/

展开阅读全文
相关内容