["a","b","c"]">

字符串数组去重 ["a","b","c","a","b","c"] --> ["a","b","c"]

非正则实现: 

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"]

转载于:https://www.cnblogs.com/MartinLee/p/7622513.html

你可能感兴趣的:(字符串数组去重 ["a","b","c","a","b","c"] --> ["a","b","c"])