美文网首页
JS数组操作:去重,交集,并集,差集

JS数组操作:去重,交集,并集,差集

作者: 安北分享 | 来源:发表于2022-03-18 10:54 被阅读0次

    去重

    1647571772.jpg

    new map + filter

    new map + filter

    //定义常量 res,值为一个Map对象实例
    //返回arr数组过滤后的结果,结果为一个数组
    //过滤条件是,如果res中没有某个键,就设置这个键的值为
    const res = new Map();
    const arr = [1,1,2,2,3,4,4,5,6,6,6,6,4,3];
    const result = arr.filter((a) => !res.has(a) && res.set(a, 1));
    console.log(result); //[1,2,3,4,5,6]
    复制代码
    

    Array from + new Set

    Array from + new Set

    
    //通过Set对象,对数组去重,结果又返回一个Set对象
    //通过from方法,将Set对象转为数组
    
    const arr = [1,1,2,2,3,4,4,5,6,6,6,6,4,3];
    const result = Array.from(new Set(arr));
    console.log(result) //[1,2,3,4,5,6];
    复制代码
    

    ES6扩展(...) + new Set

    ES6扩展(...) + new Set

    //通过Set对象,对数组去重,结果又返回一个Set对象
    //通过es6扩展方法,拷贝参数对象的所有可遍历属性到新数组之中
    
    const arr = [1,1,2,2,3,4,4,5,6,6,6,6,4,3];
    const result = [...new Set(arr)];
    console.log(result) //[1,2,3,4,5,6];
    复制代码
    

    并集

    concat + filter + includes

    concat + filter + includes

    let a = [1, 2, 3];
    let b = [2, 4, 5];
    let union = a.concat(b.filter(v => !a.includes(v)));
    console.log(union) // [1,2,3,4,5]
    复制代码
    

    Array.from + new Set + concat

    Array.from + new Set + concat

    
    // ES6中新增的一个Array.from方法,用于将类数组对象和可遍历对象转化为数组,
    // 只要类数组有length长度,基本都可以转化为数组。结合Set结构实现数学集求解
    let a = [1, 2, 3];
    let b = [2, 4, 5];
    let aSet = new Set(a);
    let bSet = new Set(b)
    // 并集
    let union = Array.from(new Set(a.concat(b)));
    console.log(union); // [1,2,3,4,5]
    复制代码
    

    concat + filter + indexOf

    concat + filter + indexOf

    不考虑NaN(数组中不含NaN)

    
    // 不考虑NAN(数组中不含NaN)
    let a = [1, 2, 3];
    let b = [2, 4, 5];
    let union = a.concat(b.filter(v=> a.indexOf(v) === -1));
    console.log(union); // [1,2,3,4,5]
    复制代码
    

    考虑NaN(数组中含NaN)

    
    let a = [1, 2, 3, NaN];
    let b = [2, 4, 5, NaN];
    let aHasNaN = a.some(v => isNaN(v));
    let bHasNaN = b.some(v => isNaN(v));
    // 并集
    let union = a.concat(b.filter(v=> a.indexOf(v) === -1 && !isNaN(v)))
                 .concat(!aHasNaN & bHasNaN ? [NaN] : []);
    
    console.log(union); // [1, 2, 3, NaN, 4, 5]
    复制代码
    

    交集

    filter + includes

    filter + includes

    
    let a = [1, 2, 3];
    let b = [2, 4, 5];
    let intersection = a.filter(v => b.includes(v));
    console.log(intersection); // [2]
    复制代码
    

    Array.from + new Set + filter

    Array.from + new Set + filter

    
    let a = [1, 2, 3];
    let b = [2, 4, 5];
    let aSet = new Set(a);
    let bSet = new Set(b);
    // 交集
    let intersection = Array.from(new Set(a.filter(v => bSet.has(v))));
    console.log(intersection); // [2]
    复制代码
    

    filter + indexOf

    filter + indexOf

    不考虑NaN(数组中不含NaN)

    
    // 不考虑NAN(数组中不含NaN)
    let a = [1, 2, 3];
    let b = [2, 4, 5];
    let intersection = a.filter(v=> b.indexOf(v) > -1);
    console.log(intersection); // [2]
    复制代码
    

    考虑NaN(数组中含NaN)

    
    // 考虑NaN(数组中含NaN)
    let a = [1, 2, 3, NaN];
    let b = [2, 4, 5, NaN];
    let aHasNaN = a.some(v => isNaN(v));
    let bHasNaN = b.some(v => isNaN(v));
    let intersection = a.filter(v=> b.indexOf(v) > -1)
                        .concat(aHasNaN & bHasNaN ? [NaN] : []);
    
    console.log(intersection); // [2, NaN]
    复制代码
    

    差集

    concat + filter + includes

    concat + filter + includes

    
    let a = [1, 2, 3];
    let b = [2, 4, 5];
    let difference = a.concat(b)
                      .filter(v => !a.includes(v) || !b.includes(v));
    
    console.log(difference)// [1,3,4,5]
    复制代码
    

    Array.from + new Set + concat + filter

    Array.from + new Set + concat + filter

    let a = [1, 2, 3];
    let b = [2, 4, 5];
    let aSet = new Set(a);
    let bSet = new Set(b)
    // 差集
    let difference = Array.from(new Set(a.concat(b).filter(v => !aSet.has(v) || !bSet.has(v))));
    
    console.log(difference) // [1,3,4,5]
    复制代码
    

    filter + indexOf + concat

    filter + indexOf + concat

    不考虑NaN(数组中不含NaN)

    
    // 不考虑NaN(数组中不含NaN)
    let a = [1, 2, 3];
    let b = [2, 4, 5];
    let difference = a.filter(v=> b.indexOf(v) === -1)
                      .concat(b.filter(v=> a.indexOf(v) === -1));
    
    console.log(difference) // [1,3,4,5]
    复制代码
    

    考虑NaN(数组中含NaN)

    
    // 考虑NaN(数组中含NaN)
    let a = [1, 2, 3, NaN];
    let b = [2, 4, 5];
    let aHasNaN = a.some(v=> isNaN(v));
    let bHasNaN = b.some(v=> isNaN(v));
    // 差集
    let difference = a.filter(v => b.indexOf(v) === -1 && !isNaN(v))
                      .concat(b.filter(v=> a.indexOf(v) === -1 && !isNaN(v))).concat(aHasNaN ^ bHasNaN ? [NaN] : []);
    
    console.log(difference) // [1, 3, 4, 5, NaN]
    

    原文转载自(https://juejin.cn/post/6844904106218225677)[JS数组操作:去重,交集,并集,差集]

    相关文章

      网友评论

          本文标题:JS数组操作:去重,交集,并集,差集

          本文链接:https://www.haomeiwen.com/subject/rdeidrtx.html