[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 ]