don*_*ald 9 javascript node.js coffeescript
我有两个数组:
array1 = ["hello","two","three"]
array2 = ["hello"]
Run Code Online (Sandbox Code Playgroud)
我想检查array2是否包含1个或更多array1字.
我怎么能用Coffeescript做到这一点?
Anu*_*rag 12
找到了一种使用这个CoffeeScript 章节检查两个数组之间交集的方法.CoffeeScript看起来非常棒.
如果在元素交集之后得到的数组包含至少一个项,则两个数组都具有公共元素.
intersection = (a, b) ->
[a, b] = [b, a] if a.length > b.length
value for value in a when value in b
x = ["hello", "two", "three"]
y = ["hello"]
intersection x, y // ["hello"]
Run Code Online (Sandbox Code Playgroud)
在这里试试吧.
以为我会抛出自己的咖啡因单线疯狂:-P
true in (val in array1 for val in array2)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4434 次 |
| 最近记录: |