版权声明:转载需注明来源 https://blog.csdn.net/Xiao_Spring/article/details/79283085
需求
返回a数组中不存在于b数组的值
输入:两个数组
输出:a数组中不存在于b数组的值
代码
/*
返回a数组中不存在于b数组的值
*/
const difference = (a,b) =>{
const s = new Set(b);
return a.filter(x=>!s.has(x));
}
console.log('a([1,2,5,6],b[1,4,6,8]) 测试结果为:'+difference([1,2,5,6],[1,4,6,8]));
效果
一点想法
利用Set的去重特征,先去优化数组b,然后利用Array.filter()方法过滤数组a
Github地址
https://github.com/YuanshuaiHuang/JS-/blob/master/code/20180207