使用reduce方法数组去重

<script>
  let a = [1,2,3,4,2,42,1,5,23,212,13,42,4]
  let b = a.reduce((prev,cur)=> {
    prev.indexOf(cur) === -1 && prev.push(cur)
    return prev
  },[])
  console.log(b);  // [1,2,3,4,42,5,23,212,13]
</script>

 

posted @ 2020-11-27 14:18  纯白棒球帽  阅读(110)  评论(0编辑  收藏  举报