数组去重

数组去重复的方法有哪些

1.使用 set

functionuniquearray(array){  let unique_array=Array.from(set(array))returnunique_array;}

2.使用 filter

functionunque_array(arr){letunique_array = arr.filter(function(elem, index, self){returnindex == self.indexOf(elem);  })returnunique_array;}console.log(unique_array(array_with_duplicates));

3.使用 for 循环

Arraydups_names = ['Ron','Pal','Fred','Rongo','Ron'];functiondups_array(dups_names){ let unique = {}; names.forEach(function(i){If(!unique[i]) {      unique[i] =true;    }  });returnObject.keys(unique);}// Ron, Pal, Fred, RongoDups_array(names);

你可能感兴趣的:(数组去重)