js使用技巧

来源:互联网 发布:开博软件注册码 编辑:程序博客网 时间:2024/05/29 16:23

一、获取数组中最大值

       apply的参数数组化来获取数组的最大值

       1)

       function getMax(arr){

if(arr){

if(arr.length==1){

return arr[0];

}else{

return Math.max.apply(null,arr);

}

}

}

        2)

Array.prototype.max = function(){
return Math.max.apply(null,this);
}

二、数组去重

Array.prototype.distinct = function(){

var result = [];

var json = {};

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

if(!json[this[i]]){

result.push(this[i]);

json[i].push(this[i]) = 1;

}

}

return result;

}

原创粉丝点击