new Set

set数组去重-LMLPHP

      const arr = [{name:"wo"},{name:"shi"},{name:"wo"}]
console.log([...new Set(arr.map(item=>item.name))].length)//
if([...new Set(arr.map(item=>item.name))].length < arr.length){
let arr2 = [...new Set(arr.map(item=>item.name))]
console.log(arr2)//["wo", "shi"]
}

文献:

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Set

05-28 20:42