[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 @   Zhentiw  阅读(45)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-11-05 [Kotlin] Adding the Hibernate dependencies to our project and creating the database
2020-11-05 [ES7] Getter and setter with Object.freeze
2020-11-05 [ES7] Private, Static class Members
2020-11-05 [Javascript] Constructor Functions and prototype
2020-11-05 [React Performance] Use CSS Variables instead of React Context?
2020-11-05 [Kotlin Spring boot] A simple demo app
2020-11-05 [Kotlin Spring boot] Dependency injection
点击右上角即可分享
微信分享提示