数组和对象数组去重

对象数组去重:

因为map key唯一,所以你想要根据哪个属性去重,res.has(a)和set(a, 1) 里a就可以改成什么,比如a.name

function unique(arr) {
    const res = new Map();
    return arr.filter((a) => !res.has(a) && res.set(a, 1))
}

数组去重

let arr = [1, 1, 4, 50, 50, 6, 2, 2]
let res = Array.from(new Set(arr))
console.log(res)

你可能感兴趣的:(数组和对象数组去重)