This is such a natural necessity, I often prototype my arrays using the 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 this anywhere:
array = [..., "Hello", "World", "Again", ...] ref = array.remove("World") alert array # [..., "Hello", "Again", ...] alert ref # "World"
This way you can also delete 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 Dec 15 '12 at 20:09 2012-12-15 20:09
source share