美文网首页es6
数组的差值

数组的差值

作者: 曼珠沙华_521b | 来源:发表于2019-03-29 15:59 被阅读0次

    let set1 = new Set(['a','b','c','d','e','f']);
    let set2 = new Set(['d','e','f','g','h','i']);

    //并集
    let union = new Set([...set1,...set2]);
    //[a,b,c,d,e,f,g,h,i]
    //交集
    let intersect = new Set([...set1].filter(x => set2.has(x)));
    //[d,e,f]
    //差集
    let dfrcset = new Set([...set1].filter(x => !set2.has(x)));

    最近在网上看到,不知道我的总是报错
    在进行差值的校验
    自己总结了一个:
    /**

    • arr1数组1
      *arr2数组2
      **/
      getDifference(arr1,arr2){
      return arr1.concat(arr2).filter((v,i ,arr)={
      return arr.indexOf(v)===arr.lastIndexOf(v)
      })
      进行调用
      this.getDifference(arr1,arr2)

    let a=[1,2,3,4,5];

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

    一直接用filter 和contact

    //交集
    let c=a.filter(res=>{return b.indexOf(res)>-1})
    //差集
    let d=a.filter(res=>{return b.indexOf(res)==-1})
    //补集
    let e=a.fliter(res=>{return !(b.indexOf(res)>-1)}).contact(b.indexOf(res=>{return !(a.indexOf(res)>-1)}))
    //并集
    let d=a.contact(b.filter(res=>{return !(a.indexOf(res)>-1)}))
    

    在遍历中操作数组

    
    let set=new Set([1,2,3]);
    set =new Set([...set].map(val=>val*2)) //2,4,6
    或者是
    let a=new Set([1,2,3]);
    a=new Set(Array.from(a, val=>val *2)) // 值是2,4,6
    

    二.对 Array 进行扩展

    
    //数组功能扩展
    //数组迭代函数
    Array.prototype.each = function(fn){
      fn = fn || Function.K;
       var a = [];
       var args = Array.prototype.slice.call(arguments, 1);
       for(var i = 0; i < this.length; i++){
           var res = fn.apply(this,[this[i],i].concat(args));
           if(res != null) a.push(res);
       }
       return a;
    };
     
    //数组是否包含指定元素
    Array.prototype.contains = function(suArr){
      for(var i = 0; i < this.length; i ++){
          if(this[i] == suArr){
              return true;
          }
       }
       return false;
    }
     
    //不重复元素构成的数组
    Array.prototype.uniquelize = function(){
       var ra = new Array();
       for(var i = 0; i < this.length; i ++){
          if(!ra.contains(this[i])){
              ra.push(this[i]);
          }
       }
       return ra;
    };
     
    //两个数组的交集
    Array.intersect = function(a, b){
       return a.uniquelize().each(function(o){return b.contains(o) ? o : null});
    };
     
    //两个数组的差集
    Array.minus = function(a, b){
       return a.uniquelize().each(function(o){return b.contains(o) ? null : o});
    };
     
    //两个数组的补集
    Array.complement = function(a, b){
       return Array.minus(Array.union(a, b),Array.intersect(a, b));
    };
     
    //两个数组并集
    Array.union = function(a, b){
       return a.concat(b).uniquelize();
    };
    

    样例:

    ar a = [1,2,3,4,5]
    var b = [2,4,6,8,10]
    console.log("数组a:", a);
    console.log("数组b:", b);
    console.log("a与b的交集:", Array.intersect(a, b));
    console.log("a与b的差集:", Array.minus(a, b));
    console.log("a与b的补集:", Array.complement(a, b));
    console.log("a与b的并集:", Array.union(a, b));
    

    三:es6

    
    var a = [1,2,3,4,5]
    var b = [2,4,6,8,10]
    console.log("数组a:", a);
    console.log("数组b:", b);
     
    var sa = new Set(a);
    var sb = new Set(b);
     
    // 交集
    let intersect = a.filter(x => sb.has(x));
     
    // 差集
    let minus = a.filter(x => !sb.has(x));
     
    // 补集
    let complement  = [...a.filter(x => !sb.has(x)), ...b.filter(x => !sa.has(x))];
     
    // 并集
    let unionSet = Array.from(new Set([...a, ...b]));
     
    console.log("a与b的交集:", intersect);
    console.log("a与b的差集:", minus);
    console.log("a与b的补集:", complement);
    console.log("a与b的并集:", unionSet);
    

    四:利用jq

    var a = [1,2,3,4,5]
    var b = [2,4,6,8,10]
    console.log("数组a:", a);
    console.log("数组b:", b);
     
    var sa = new Set(a);
    var sb = new Set(b);
     
    // 交集
    let intersect = a.filter(x => sb.has(x));
     
    // 差集
    let minus = a.filter(x => !sb.has(x));
     
    // 补集
    let complement  = [...a.filter(x => !sb.has(x)), ...b.filter(x => !sa.has(x))];
     
    // 并集
    let unionSet = Array.from(new Set([...a, ...b]));
     
    console.log("a与b的交集:", intersect);
    console.log("a与b的差集:", minus);
    console.log("a与b的补集:", complement);
    console.log("a与b的并集:", unionSet);
    

    部分转自[hangge] (http://www.hangge.com/blog/cache/detail_1862.html)

    相关文章

      网友评论

        本文标题:数组的差值

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