Javascript 在 CoffeeScript 中从数组中删除一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8205710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove a value from an array in CoffeeScript
提问by ajsie
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"?
如何检查“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:
感谢您的建议。我找到了一个很酷的方法:
回答by Ricardo Tomasi
filter()
is also an option:
filter()
也是一种选择:
arr = [..., "Hello", "World", "Again", ...]
newArr = arr.filter (word) -> word isnt "World"
回答by Ry-
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.
array.indexOf("World")
将获得"World"
或-1
如果它不存在的索引。array.splice(indexOfWorld, 1)
将从"World"
数组中删除。
回答by Alvaro Louren?o
For this is such a natural need, I often prototype my arrays with an remove(args...)
method.
由于这是一个如此自然的需求,我经常用一种remove(args...)
方法来对我的数组进行原型设计。
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"]
回答by catvir
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)
保持参考(这是我发现的最短的 - !.push 总是假的,因为 .push > 0)
refs = []
array = array.filter (e) -> e != 'World' || !refs.push e
回答by xpou
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"]
回答by Alex L
A combination of a few answers:
几个答案的组合:
Array::remove = (obj) ->
@filter (el) -> el isnt obj
回答by Ivan Aranibar
_.without()
function from the underscorejslibrary is a good and clean option in case you want to get a new array :
_.without()
如果您想获取新数组,则underscorejs库中的函数是一个不错的选择:
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1)
[2, 3, 4]
回答by Igor Teterin
CoffeeScript + jQuery: remove one, not all
CoffeeScript + jQuery:删除一个,而不是全部
arrayRemoveItemByValue = (arr,value) ->
r=$.inArray(value, arr)
unless r==-1
arr.splice(r,1)
# return
arr
console.log arrayRemoveItemByValue(['2','1','3'],'3')