[英]How can i find all the duplicates in from an array without using ES6 features?
我试图在不使用 es6 功能的情况下做到这一点,但每次我最终都使用 es6。 是不是一定有什么办法可以实现。
不确定您在谈论哪些 ES6 功能,但仅使用简单的逻辑应该是相当直接的(没有测试代码,只是试图解释一个概念)
const arr = [1, 2, 3, 4, 2]
const found = []
arr.forEach(el => {
if(found.includes(el)) {
console.warn('Found duplicate: ', el)
return
}
found.push(el)
})
您基本上可以跟踪您已经找到的元素列表,并在该元素再次出现时执行某些操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.