Manon.icu

I'm here to make you a better developer by teaching you everything I know about building for the web.

Published 2020-04-19

数组去重

3 种数组去重方法

Set

const arr=[1,2,🤩,🤩,🤩,2,1]
console.log([...new Set(arr)])

filter

const arr=[1,2,🤩,🤩,🤩,2,1]
arr.filter((item,index)=>arr.indexOf(item)===index)

reduce

const arr=[1,2,🤩,🤩,🤩,2,1]
arr.reduce((unique,item)=>unique.includes(item)?unique:[...unique,item],[])

Comments

No Comments!