Array · 数组去重

182 阅读1分钟
  • 利用ES6中提供的Set数据结构对字符串(s1+s2)“去重”,然后结构赋值得到数组,最后进行排序并转成字符串。
const list = (s1, s2) => [...new Set(s1+s2)].sort().join('')
  • 1
let list = Array.from(new Set(oldList));
  • 2
let list = [...new Set(oldList)];
  • 3 indexOf
 function unique(arr){
     var newArr = [];
     for(var i in arr) {
         if(newArr.indexOf(arr[i]) == -1) {
             newArr.push(arr[i])
         }
     }
     return newArr;
 }