Coffeescript:数组元素与另一个数组相匹配

我有两个数组:

array1 = ["hello","two","three"] array2 = ["hello"] 

我想检查array2是否包含1个或更多的array1单词。

我怎样才能使用Coffeescript?

find一种方法来检查使用这个CoffeeScript 章节的两个数组之间的交集。 CoffeeScript看起来非常棒。

如果元素相交后的数组至less包含一个元素,那么两个数组都有公共元素。

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

在这里尝试。

我以为我会扔我自己的咖啡的单线疯狂: – P

 true in (val in array1 for val in array2) 
 contains = (item for item in array2 when item in array1) 

(反转数组以在array1显示双重条目)

我做了一个函数is_in,看看我的例子:

 array1 = ["hello","two","three"] array2 = ["hello"] is_in = (array1, array2) -> for i in array2 for j in array1 if i is j then return true console.log is_in(array1, array2) 

在这里testing

看看十字路口的例子之后,我可以通过另一种方式来实现:

 intersection = (a, b) -> [a, b] = [b, a] if a.length > b.length return true for value in a when value in b array1 = ["hello","two","three"] array2 = ["hello"] console.log intersection(array1, array2) 

在这里testing

你可以尝试:

 (true for value in array1 when value in array2).length > 0 

以防万一有人来这里,正在寻找差异,而不是相交

 difference = (val for val in array1 when val not in array2) 

这会给你一个array1中所有值的数组( 不同 ),但是不会在array2中