我试图在不使用es6特性的情况下做到这一点,但每次我最后都使用es6。必须有任何方法来实现。
发布于 2022-05-18 18:20:39
不确定您正在讨论的是哪些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)
})您基本上可以跟踪已经找到的元素列表,并在该元素再次出现时执行一些操作。
https://stackoverflow.com/questions/72294066
复制相似问题