Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
56
rated 0 times [  57] [ 1]  / answers: 1 / hits: 37750  / 13 Years ago, sun, november 20, 2011, 12:00:00

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


More From » node.js

 Answers
37

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.


[#89004] Friday, November 18, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
devane

Total Points: 451
Total Questions: 88
Total Answers: 100

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
;