set数组去重

new Set

 

 

      const arr = [{name:"wo"},{name:"shi"},{name:"wo"}]
      console.log([...new Set(arr.map(item=>item.name))].length)//2
      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

 

posted @ 2018-01-25 11:48  最爱小虾  阅读(235)  评论(0编辑  收藏  举报