代码
function unique (arr) {
let newArr = []
let obj = {}
arr.forEach(item => {
if (typeof item !== 'object') {
if (newArr.indexOf(item) === -1) {
newArr.push(item)
}
} else {
let str = JSON.stringify(item)
if (!obj[str]) {
newArr.push(item)
obj[str] = 1
}
}
})
return newArr
}
console.log(unique([123, 123, [1,2,3], [1,'2',3], [1, 3, 4], [1,2,3], {a:1}, {a:1}, 'hello', null, null]))
输出