[Javascript] Filter out Duplicates from Flat JavaScript Array with array.filter / reduce / Set

const ary = [1, 2, 3, 4, 2, 3];

const unqiAry = (ary) => ary.filter((item, index) => ary.indexOf(item) === index)

unqiAry(ary) // [ 1, 2, 3, 4 ]

 

const ary = [1, 2, 3, 4, 2, 3];

const unqiAry = (ary) => ary.reduce((acc, curr) => acc.indexOf(curr) > -1 ? acc : [...acc, curr] , [])

unqiAry(ary) // [1,2,3,4]

 

const ary = [1, 2, 3, 4, 2, 3];

const unqiAry = (ary) => [...new Set(ary)]

unqiAry(ary) // [ 1, 2, 3, 4 ]

 

posted @ 2021-11-05 15:36  Zhentiw  阅读(45)  评论(0编辑  收藏  举报