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();
    }
  • 相关阅读:
    一元多项式乘法
    将博客搬至CSDN
    Tomcat的几种部署方式
    Visual Studio 2012以后无法保存只读文件的问题
    WPF中的Generic.xaml, theme以及custom control
    WPF的页面导航
    WPF MVVM系列文章
    tomcat中同时部署两个项目的问题
    Windows多线程系列
    XML DTD和XML Schema
  • 原文地址:https://www.cnblogs.com/tiger95/p/7345586.html
Copyright © 2011-2022 走看看