标签:function concat ons func 唯一性 方法 lte indexof ===
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))
标签:function concat ons func 唯一性 方法 lte indexof ===
原文地址:https://www.cnblogs.com/helloNico/p/12183260.html