Remove a value from an array in CoffeeScript


I have an array:

array = [..., "Hello", "World", "Again", ...]

How could I check if "World" is in the array? Then remove it if it exists? And have a reference to "World"?

Sometimes maybe I wanna match a word with a regexp and in that case I won't know the exact string so I need to have a reference to the matched String. But in this case I know for sure it's "World" which makes it simpler.

Thanks for the suggestions. I found a cool way to do it:

3/25/2017 5:32:24 PM

Accepted Answer

array.indexOf("World") will get the index of "World" or -1 if it doesn't exist. array.splice(indexOfWorld, 1) will remove "World" from the array.

11/20/2011 11:38:28 PM

filter() is also an option:

arr = [..., "Hello", "World", "Again", ...]

newArr = arr.filter (word) -> word isnt "World"

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow