Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

http://documentcloud.github.com/underscore

like image 206
ajsie Avatar asked Nov 20 '11 23:11

ajsie


People also ask

How do I remove a specific value from an array?

pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.

How do I remove an element from an array in node?

Array elements can be deleted using the JavaScript operator delete . Using delete leaves undefined holes in the array. Use pop() or shift() instead.


1 Answers

filter() is also an option:

arr = [..., "Hello", "World", "Again", ...]  newArr = arr.filter (word) -> word isnt "World" 
like image 139
Ricardo Tomasi Avatar answered Oct 19 '22 07:10

Ricardo Tomasi