zoukankan      html  css  js  c++  java
  • js Array 阵列扩展方法

    
    //又来了
      Array.prototype.unique = function()
     {
        this.sort();
        var re=[this[0]];
        for(var i = 1; i < this.length; i++)
        {
            if( this[i] !== re[re.length-1])
            {
                re.push(this[i]);
            }
        }
        return re;
     }
    
     //并集
     Array.prototype.union = function(a)
     {
       return this.concat(a).unique(); 
    
     }
     //差集
     Array.prototype.minus = function(a)
     { 
        var result =[];
        var clone = this;
          for(var i=0; i < clone.length; i++)
          {
              var flag = true; 
              for(var j=0; j < a.length; j++)
              {   
                if(clone[i] == a[j])   
                flag = false;   
              }   
            if(flag)   
            result.push(clone[i]); 
    
          }
    
        return result.unique(); 
    
     }
    // 交集
    Array.prototype.intersect = function(b) { 
        var result = [];
        var a = this;
        for(var i = 0; i < b.length; i ++) {
            var temp = b[i];
            for(var j = 0; j < a.length; j ++) {
                if(temp === a[j]) {
                    result.push(temp);
                    break;
                }
            }
        }
        return result.unique();
    }

    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    LightOJ
    Peter and Snow Blower
    Gena's Code
    nyoj139--我排第几个 (康拓展开)
    hdoj1394(归并排序)
    树状数组
    Poj 1113--Wall(凸集)
    hdoj1437 -- 天气情况
    hdoj1428 -- 漫步校园 (记忆化搜索)
    图像边缘检测
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4891510.html
Copyright © 2011-2022 走看看