方式一, 借助ES6的Map, 因?yàn)镸ap不會(huì)出現(xiàn)重復(fù)的key, 后添加的key-value會(huì)把之前的覆蓋
var arr = [1,2,3,4,3,3,2,5]
var map = new Map()
arr.forEach((value,index) => {
map.set(value,index)
})
var newArr = Array.form(map.keys())
console.log(newArr)
方式二
var arr = [1,2,3,4,3,3,2,5]
var newArr = []
arr.forEach((value,index) => {
if(newArr.indexOf(value) == -1){
newArr.push(value)
}
})
console.log(newArr)
還有什么更好的方式,請(qǐng)大家舉薦