jquery 去掉重复项(splice,apply,push)

时间:2022-12-18 17:04:03
 /*
js数组去掉重复项
var somearray = [1,1,2,2,3,3,4,4,'1'];
somearray.check();
//somearray will return arr=[1,2,3,4,'1']
*/
Array.prototype.check= function(){
var tem=[];
this.forEach(function(value){
if(tem.indexOf(value)===-1){
tem.push(value);
}
});
this.splice(0);
this.push.apply(this,tem);
}

注:与数字相同的数字字符无法区分,比如【'1'】和【1】