1、reduce
const obj = {}
const arr = [
{code: 1},
{code: 1},
{code: 2}
]
let newArr = arr.reduce((pre,next)=>{
obj[next.code] ? '' : obj[next.code] = true && pre.push(next)
return pre
},[])
console.log(newArr)
2、forEach
const arr = [
{code: 1},
{code: 1},
{code: 2}
]
let obj = {}
let newArr = []
arr.forEach(item=>{
obj[item.code] = item
})
for(let i in obj){
newArr.push(obj[i])
}
console.log(newArr)
3、map + JSON.stringify
const arr = [
{code: 1},
{code: 1},
{code: 2}
]
let tempArr = arr.map( item => JSON.stringify(item) )
tempArr = Array.from(new Set(tempArr)).map(item=> JSON.parse(item))
console.log("tempArr:::", tempArr)