对象数组的去重方式
var arr = [
{
name: 'ZYTX',
age: '6zu-Rtnw4Ix',
gender: 'AAAAAA.doc'
},
{
name: 'ZYTA',
age: '6zu-Rtnw4Ix',
gender: 'BBBBBB.doc'
},
{
name: 'ZDTX',
age: '6zu-Rtnw4Ix',
gender: 'CCCCCC.doc'
},
{
name: 'ZYTX',
age: '6zu-Rtnw4Ix',
gender: 'AAAAAA.doc'
}
]
function uniqueFunc(arr, uniId) {
const res = new Map()
return arr.filter(
(item) => !res.has(item[uniId]) && res.set(item[uniId], 1)
)
}
function uniqueFunc2(arr, uniId) {
let hash = {}
return arr.reduce((accum, item) => {
hash[item[uniId]] ? '' : (hash[item[uniId]] = true && accum.push(item))
return accum
}, [])
}
function uniqueFunc3(arr, uniId) {
let obj = {}
let tempArr = []
for (var i = 0; i < arr.length; i++) {
if (!obj[arr[i][uniId]]) {
tempArr.push(arr[i])
obj[arr[i][uniId]] = true
}
}
return tempArr
}