标签:
1 function removeDuplicates(arr) { 2 3 var temp = {}, r = []; 4 5 for (var i in arr) 6 7 temp[arr[i]] = true; 8 9 for (var k in temp) 10 11 r.push(k); 12 13 return r; 14 15 } 16 17 //用法 18 19 var fruits = [‘apple‘, ‘orange‘, ‘peach‘, ‘apple‘, ‘strawberry‘, ‘orange‘, ‘strawberry‘, ‘orange‘]; 20 21 var uniquefruits = removeDuplicates(fruits); 22 alert(uniquefruits);
字符串去重方法:
采用正则
/**
* 字符串去重
* \r\n字符串分隔符
* $1分割后的字符串,$2字符串的索引
* 以分隔符将字符串分割,根据分割后的数组元素的个数进行循环比较
*/
function strUnique(){
var str = "abc, abcd, abc, abcde, abcd, abcde";
var ret = [];
str.replace(/[^,]+/g, function($1, $2){
(str.indexOf($1) == $2) && ret.push($1);
});
alert(ret);
return ret;
}
1 function strUnique(){ 2 var str = "abc, abcd, abc, abcde, abcd, abcde"; 3 var ret = []; 4 str.replace(/[^,]+/g, function($1, $2){ 5 (str.indexOf($1) == $2) && ret.push($1); 6 }); 7 alert(ret); 8 return ret; 9 } 10 11 strUnique();
标签:
原文地址:http://www.cnblogs.com/double405/p/5174763.html