There are many questions about this, not least: jQuery version of array contains, a solution with the splice method and many more. However, they all seem complicated and annoying.
With the combined powers of javascript, jQuery and coffeescript, what is the very cleanest way to remove an element from a javascript array? We don't know the index in advance. In code:
a = [4,8,2,3]
a.remove(8) # a is now [4,2,3]
Failing a good built-in method, what is a clean way of extending javascript arrays to support such a method? If it helps, I'm really using arrays as sets. Solutions will ideally work nicely in coffeescript with jQuery support. Also, I couldn't care less about speed, but instead prioritize clear, simple code.
This is just a slight change to Amir's awesome solution:
which returns the element iff the list has it, so you can do something like:
The remove coffee script translates to this javascript:
Using vanilla Javascript:
Only jQuery: