从 CoffeeScript 中的数组中删除一个值

发布于 2024-12-17 04:19:29 字数 418 浏览 0 评论 0原文

我有一个数组:

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

如何检查“World”是否在数组中? 如果存在的话将其删除? 并提到“世界”?

有时我可能想用正则表达式匹配一个单词,在这种情况下我不知道确切的字符串,所以我需要引用匹配的字符串。但在这种情况下,我确信它是“世界”,这使得它更简单。

感谢您的建议。我找到了一个很酷的方法:

http://documentcloud.github.com/underscore

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

娇纵 2024-12-24 04:19:29

filter() 也是一个选项:

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

newArr = arr.filter (word) -> word isnt "World"

filter() is also an option:

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

newArr = arr.filter (word) -> word isnt "World"
反差帅 2024-12-24 04:19:29

array.indexOf("World") 将获得"World" 的索引,如果不存在则为 -1array.splice(indexOfWorld, 1) 将删除 < code>“World” 来自数组。

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.

虐人心 2024-12-24 04:19:29

由于这是一种自然的需求,我经常使用 remove(args...) 方法对数组进行原型设计。

我的建议是将其写在某个地方:

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

并在任何地方使用这样的方法:

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

这样您还可以同时删除多个项目:

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

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"]
饮惑 2024-12-24 04:19:29

检查“World”是否在数组中:

"World" in array

删除是否存在

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

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

保留引用(这是我发现的最短的 - !.push 始终为 false,因为 .push > 0)

refs = []
array = array.filter (e) -> e != 'World' || !refs.push e

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
匿名的好友 2024-12-24 04:19:29

试试这个:

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"]

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"]
山色无中 2024-12-24 04:19:29

几个答案的组合:

Array::remove = (obj) ->
  @filter (el) -> el isnt obj

A combination of a few answers:

Array::remove = (obj) ->
  @filter (el) -> el isnt obj
断舍离 2024-12-24 04:19:29

underscorejs 库中的 _.without() 函数是一个很好且干净的选项如果你想获得一个新数组:

_.without([1, 2, 1, 0, 3, 1, 4], 0, 1)
[2, 3, 4]

_.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]
南风几经秋 2024-12-24 04:19:29

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')

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文