// 数组去重(基本类型 复杂类型)
const arr = [123, 123, {a: 1, b: 2}, {b: 2, a: 1}, {a: '1'}, [1, '2', 3], [1, 2, 3], [1, '2', 3]]
// 输出 [123, {a: 1}, {a: '1'}, [1, '2', 3], [1, 2, 3]]
const hash = (arg) => {
if(typeof arg === 'string' || typeof arg === 'undefined') {
return `'${arg}'`
}
if(typeof arg === 'number'){
return `${arg}`
}
if(typeof arg === null){
return 'null'
}
if(Array.isArray[arg]){
let res = '['
arg.map(item => {
return res += hash(item)
})
res += ']'
return res
}
let res = '{'
const keys = Object.keys(arg).sort()
keys.map(k => {
res += `${k}:${hash(arg[k])}`
})
res += '}'
return res
}
const unique = (arr) => {
let map = {}
return arr.filter((item) => {
let key = hash(item)
return map[key] ? false : (map[key] = true)
})
}
console.log(unique(arr))
console