首页 > 编程语言 >JavaScript 数组求差集

JavaScript 数组求差集

时间:2023-02-12 13:12:39浏览次数:44  
标签:数组 求差 JavaScript arrayDiff let arr2 arr1

let arr1 = [1,2,3,4,5];
let arr2 = [4,5,6,7,8];

//数组求差集
function arrayDiff(arr1,arr2) {
    //先去重
    let arr1Unique = [...new Set(arr1)];
    let arr2UniqueSet = new Set(arr2);

    return arr1Unique.filter(item=>!(arr2UniqueSet.has(item)));
}

console.log(arrayDiff(arr1,arr2));
console.log(arrayDiff(arr2,arr1));

标签:数组,求差,JavaScript,arrayDiff,let,arr2,arr1
From: https://www.cnblogs.com/cqpanda/p/17083302.html

相关文章