- Array.prototype.unique = function () { //array原型扩展
- return this.sort().join(",,").replace(/(,|^)([^,]+)(,,)+(,|$)/g,"$1$2$4").replace(/,,+/g,",").replace(/,$/,"").split(",");
- }
- //使用方法举例
- var arr = ["a","b","c","a","d","e","c","f","d"];
- var newArr = arr.unique(); //["a","b","c","d","e","f"]
- function ArrUnique(arr){
- var newArr = [];
- for(var i=0;i< arr.length;i++){
- if(newArr.toString().match(arr[i]) == null){
- newArr.push(arr[i]);
- }
- }
- return newArr;
- }
- var myArr=["a","b","c","a","d","e","c","f","d"];
- var myNewArr = ArrUnique(myArr); //["a","b","c","d","e","f"]
说明:toString()数组转换成字符串;match()匹配字符串,返回值:null或匹配到的字符串;如果返回null表示该字符串没有重复,进而push()追加到新的数组里。
本站声明:本站部分文章来自网络,由用户上传分享,如若内容侵犯了您的合法权益,可联系我们进行处理。文章仅供大家学习与参考,不**本站立场。