48

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

Ry-
  • 218,210
  • 55
  • 464
  • 476
ajsie
  • 77,632
  • 106
  • 276
  • 381
  • 1
    http://stackoverflow.com/questions/4825812/clean-way-to-remove-element-from-javascript-array-with-jquery-coffeescript – Andrei Dec 25 '11 at 14:06

8 Answers8

73

filter() is also an option:

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

newArr = arr.filter (word) -> word isnt "World"
Ricardo Tomasi
  • 34,573
  • 2
  • 55
  • 66
  • 4
    Important difference: this solution is not destructive, i.e. `arr` will stay the same (which is usually good functional practice). Compare with accepted answer, which is destructive. – mck Jun 05 '14 at 21:20
  • In the question, he is asking to have a reference to "World" – Akhorus Jan 27 '15 at 14:38
  • Note that (awkwardly enough) this is space-sensitive; `arr.filter(word) -> word isn't "Word"` generates a JavaScript error. – ashes999 Nov 10 '15 at 20:53
  • @ashes999 it's not that awkward actually. what you write is syntactically wrong. `arr.filter (word) -> word isn't "Word"` means that "execute the filter function with a `(word) -> word isn't "Word"` function parameter" – smddzcy Sep 20 '16 at 21:20
62

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.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • 1
    The solutions below offer better coffeescript syntax. But if you only have one element to remove from the array, this workflow has better performance as it won't traverse the whole array looking for more elements with the same value. – Erik J May 07 '13 at 18:34
  • 1
    @ErikJ: “Better” CoffeeScript syntax? – Ry- May 07 '13 at 19:19
  • 2
    @ryanh: You're right, better isn't the right word, but the others offered alternatives that take advantage of CoffeeScript-specific features – Erik J May 07 '13 at 21:20
17

For this is such a natural need, I often prototype my arrays with an remove(args...) method.

My suggestion is to write this somewhere:

Array.prototype.remove = (args...) ->
  output = []
  for arg in args
    index = @indexOf arg
    output.push @splice(index, 1) if index isnt -1
  output = output[0] if args.length is 1
  output

And use like this anywhere:

array = [..., "Hello", "World", "Again", ...]
ref = array.remove("World")
alert array # [..., "Hello", "Again",  ...]
alert ref   # "World"

This way you can also remove multiple items at the same time:

array = [..., "Hello", "World", "Again", ...]
ref = array.remove("Hello", "Again")
alert array # [..., "World",  ...]
alert ref   # ["Hello", "Again"]
Alvaro Lourenço
  • 1,321
  • 1
  • 10
  • 21
15

Checking if "World" is in array:

"World" in array

Removing if exists

array = (x for x in array when x != 'World')

or

array = array.filter (e) -> e != 'World'

Keeping reference (that's the shortest I've found - !.push is always false since .push > 0)

refs = []
array = array.filter (e) -> e != 'World' || !refs.push e
catvir
  • 196
  • 1
  • 4
8

Try this :

filter = ["a", "b", "c", "d", "e", "f", "g"]

#Remove "b" and "d" from the array in one go
filter.splice(index, 1) for index, value of filter when value in ["b", "d"]
xpou
  • 101
  • 1
  • 4
  • 2
    This does not work as expected when deleting a second last item from the array. See http://jsfiddle.net/8v25L/ – Blaise Jul 20 '13 at 12:01
2

A combination of a few answers:

Array::remove = (obj) ->
  @filter (el) -> el isnt obj
Alex L
  • 8,748
  • 5
  • 49
  • 75
2

_.without() function from the underscorejs library is a good and clean option in case you want to get a new array :

_.without([1, 2, 1, 0, 3, 1, 4], 0, 1)
[2, 3, 4]
Ivan Aranibar
  • 2,226
  • 2
  • 18
  • 21
0

CoffeeScript + jQuery: remove one, not all

arrayRemoveItemByValue = (arr,value) ->
  r=$.inArray(value, arr)
  unless r==-1
    arr.splice(r,1)
  # return
  arr

console.log arrayRemoveItemByValue(['2','1','3'],'3')
Igor Teterin
  • 123
  • 6