Problem
You have an array in CoffeeScript, which contains equal elements multiple times such as:
[1,1,2,3,3]
You would like to have only unique values in the array. Thus, transform it into:
[1,2,3]
Solution
You can use the following method to accomplish such:
removeDuplicates = (ar) -> if ar.length == 0 return [] res = {} res[ar[key]] = ar[key] for key in [0..ar.length-1] value for key, value of res alert(removeDuplicates([1,2,3,3,4,4,5]));
References
This solution is based on this approach (with a few minor issues fixed).
Reblogged this on Learn CoffeeScript.
An alternative could be:
remove_duplicates = ( array ) ->
res = []
res.push val for val in array when val not in res
res
Oh! ehmickey’s solution is beautiful!