字符串数组去重 ["a","b","c","a","b","c"] --> ["a","b","c"]
原创
©著作权归作者所有:来自51CTO博客作者Hello_Martin的原创作品,请联系作者获取转载授权,否则将追究法律责任
非正则实现:
let str_arr=["a","b","c","a","b","c"]
function unique(arr){
return [...new Set(arr)]
}
console.log(unique(str_arr)) // ["a","b","c"]
用正则实现:
var str_arr = ["a", "b", "c", "a", "b", "c"]
function unique(arr) {
return arr.sort().join(",,").
replace(/(,|^)([^,]+)(,,\2)+(,|$)/g, "$1$2$4").
replace(/,,+/g, ",").
replace(/,$/, "").
split(",")
}
console.log(unique(str_arr)) // ["a","b","c"]