javascript 从数组中删除重复项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 从数组中删除重复项相关的知识,希望对你有一定的参考价值。

// ES6 Set
function removeDups(arr) {
	let noDup = [...new Set(arr)];
	return noDup;
}
removeDups(["John", "Taylor", "John"]) // ["John", "Taylor"]

// Reduce
function removeDups(arr) {
	let uniq = arr.reduce((a,b) => {
      if (a.indexOf(b) < 0) a.push(b);
      return a;
    },[])
    console.log(uniq)
}
removeDups(["John", "Taylor", "John"]) // ["John", "Taylor"]

以上是关于javascript 从数组中删除重复项的主要内容,如果未能解决你的问题,请参考以下文章