数组去重
Posted hellonico
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数组去重相关的知识,希望对你有一定的参考价值。
1、filter方法,接收一个函数作为参数,新建一个数组,遍历数组内的每个元素,符合条件的返回
function getOne(a,b){ var arr = a.concat(b); return arr.filter(function(item,index){ return arr.indexOf(item) === index; }) } var a = ["aaa","bbb","ccc","ddd","eee"]; var b = ["ccc","ddd","eee","fff","gg"]; console.log(getOne(a,b))
2、利用对象属性的唯一性通过for...of遍历
function getOne(a,b){ var arr1 = a.concat(b) var arr2 = []; var obj = {}; for(var i of arr1){ if(!obj[i]){ arr2.push(i) obj[i] = 111 } } return arr2 } var a = ["aaa","bbb","ccc","ddd","eee"]; var b = ["ccc","ddd","eee","fff","gg"]; console.log(getOne(a,b))
以上是关于数组去重的主要内容,如果未能解决你的问题,请参考以下文章