2014-01-17 135 views

回答

1

這不是特別的CoffeeScript十歲上下,但這樣的事情會做的伎倆:

filterAndRandomSelect = (arr1, arr2) -> 
    filtered = (i for i in arr1 when i not in arr2) #this is pretty cute 
    filtered[Math.floor(Math.random() * filtered.length)] 

console.log filterAndRandomSelect [1, 2, 3, 4, 5], ['a', 'b', 'c', 4, 5] 

當然,這「可愛」 CS線可以很容易爲:

filtered = arr1.filter (val) -> val not in arr2 

這也有點可愛。

+0

確實很可愛,謝謝! – Harry