0

I have an array with elements duplicates:

data = ["Ruby on rails", "Ruby on rails", "Jquery", "Coffescript", "Javascript"]

I try to remove duplicate elements of array with

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

But I get the same result with elements duplicates.

How can I fixed?

Thanks!

1
  • Short of me fixing your code for you I'd recommend a library like underscore.js it's great for this sort of thing. Commented Sep 27, 2013 at 14:26

1 Answer 1

5
… is "undefined"

does get compiled to === "undefined", which is not what you want. Remove the quotes for not comparing against the string, but the undefined value.

Btw, your script should be

index = {}
uniques = []

for text in data
 unless (text of index)
  index[text] = true
  uniques.push(text)

or even a filter comprehension like

index = {}
uniques = for text in data when not (text of index)
 index[text] = true
 text
Sign up to request clarification or add additional context in comments.

1 Comment

If I remove the do "undefined" and add undefined from if indexes[data[i].text] is undefined I get [][][][][] Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.