代码改变世界

JS数组的扩展

2012-04-10 19:43  刘联东  阅读(505)  评论(0编辑  收藏  举报

直接引用 array.js文件即可实现数组的功能扩展,其中包括基本的元素查找、逐个操作、过滤、最大值、平均值

同时扩展了集合交差并补操作。

如果要对数组进一步扩展只需要

Array_Object .implements(xx)

后续工作包括数组的计算工具包{ 向量计算、矩阵计算}

由于WebAPP对越来越向计算密集型发展,比如前端算法设计需要集成各种复杂的数据类型。HTML5中3D渲染需要大量的数组和矩阵计算法。因此数组、矩阵、集合的操作成为这种趋势发展的关键。为了更方便地开发计算密集型应用,模仿matlab工具包的机制设计一些底层的高效计算工具包必能够造福大量的开发者。

__________________array.js____________________________________________________

 

 

Array_Object = {

   //获得数组的num个最大项

   max : function(num){

       var bag = Array(num);

       var i = 0;

       for(i=0; i<num && i<this.length; i++){

          bag[i] = this[i];

       }

       bag.sort();

 

       var tnum, temp;

       for(i=num; i<this.length; i++){

           tnum = this[i];

           for(var j=0; j<num; j++){

               if(tnum > bag[j]){

                   temp = bag[j];

                   bag[j] = tnum;

                   tnum = temp;

               }

           }

       }

      return bag;

   },

  

   //获得数组的平均值,translate表示是否强制转化

   avg : function(translate){

      var sum = 0;

     if( translate ){

          for(var i=0; i<this.length; i++){

              sum += ~~this[i]; //防止数组中存在非数值

          }

      }else{

          for(var i=0; i<this.length; i++){

              sum += this[i]; //防止数组中存在非数值

          };

      }

      return sum /  this.length;

   },

 

    //深度拷贝 它返回一个深度拷贝的数组。如果你对此有所迷惑,请点击这里获得详细的解释。

   copy : function(){

      return [].concat(this);

   },

 

   //随机化 对一个数组进行随机排序,返回值为 undefined

   randomize : function(){

       this.sort(

           function randomSort(a , b){

               return( parseInt( Math.random()*10 ) %2 );

           }

       );

   },

 

   //索引元素在数组中的位置

   indexOf : function(el, start) {

       var start = start || 0;

       for ( var i=0; i < this.length; ++i ) {

           if ( this[i] === el ) {

               return i;

           }

       }

       return -1;

   },

 

contains : function(el){

   return this.indexOf(el) > -1;

},

 

   foreach : function(fn, domain) {

       var scope = domain || window;

       for ( var i=0, j=this.length; i < j; ++i ) {

           fn.call(scope, this[i], i, this);

       }

   },

 

 

   //如果数组中的每个元素都能通过给定的函数的测试,则返回true,反之false。换言之给定的函数也一定要返回true与false

   every : function(fn, domain) {

       var scope = domain || window;

       for ( var i=0, j=this.length; i < j; ++i ) {

       if ( !fn.call(scope, this[i], i, this) ) {

           return false;

       }

       }

       return true;

   },

   

   //类似all函数,但只要有一个通过给定函数的测试就返回true。

   some : function(fn, domain) {

       var scope = domain || window;

       for ( var i=0, j=this.length; i < j; ++i ) {

           if ( fn.call(scope, this[i], i, this) ) {

               return true;

           }

       }

       return false;

   },

 

   //把符合条件的元素放到一个新数组中返回。

   filter : function(fn, domain) {

       var scope = domain || window;

       var bag = [];

       for ( var i=0, j=this.length; i < j; ++i ) {

           if ( !fn.call(scope, this[i], i, this) ) {

               continue;

           }

           bag.push(this[i]);

       }

       return bag;

   },

 

   //让数组中的每一个元素调用给定的函数,然后把得到的结果放到新数组中返回。。

   map : function(fn, domain) {

       var scope = domain || window;

       var bag = [];

       for ( var i=0, j=this.length; i < j; ++i ) {

           bag.push(fn.call(scope, this[i], i, this));

       }

       return bag;

   },

 

   unique : function(){

        var bag = new Array();  

        for(var i = 0; i < this.length; i ++){

            if(!bag.contains(this[i])){  

               bag.push(this[i]);  

            }  

        }  

        return bag;

   },

 

   //补集

   complement : function(arr){

        return this.union(arr).diff(this.intersect(arr));

   },

 

   //交集

   intersect : function( arr){

        return this.filter(arr.contains,arr);

   },

  

   //差集

   diff : function(arr){

   return this.filter(

   function(el){

     return !arr.contains(el);

   }

);

   },

  

   //和集

   union : function(arr){

        return this.concat(arr).unique();

   },

  

   implements : function(lib){

   lib = lib || this;

       var arr = [];

       for(var item in lib){

           if(!arr[item]){ //不覆盖浏览器中原生方法

               Array.prototype[item] = lib[item];

           }

       }

   }

}.implements();

联动 互联互动