我有一个包含重复元素的数组:
data = ["Ruby on rails", "Ruby on rails", "Jquery", "Coffescript", "Javascript"]
我尝试使用删除数组的重复元素
indexes = []
uniques = []
i = 0
while i < data.length
if indexes[data[i].text] is "undefined"
indexes[data[i].text] = "defined"
uniques.push
i++
console.log data
但是我对重复的元素得到了相同的结果。
如何修复?
谢谢!
最佳答案
… is "undefined"
确实被编译为===“undefined”
,这不是你想要的。删除引号,因为不与字符串进行比较,而是与 未定义
值进行比较。
顺便说一句,你的脚本应该是
index = {}
uniques = []
for text in data
unless (text of index)
index[text] = true
uniques.push(text)
甚至是像
这样的过滤器理解index = {}
uniques = for text in data when not (text of index)
index[text] = true
text
关于javascript - 使用 CoffeeScript 删除数组的重复元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19053204/