2013-04-15 32 views
1

獲得通過陣列PARAMS模型我有這樣一個模型:如何從集合

model = 
    from: "[email protected]" 
    id: 1 
    to: [[email protected]] 

,我必須含有這些類型的模型的集合。該集合需要過濾from。我知道_.whereunderscore.js函數。我使用這樣的:

fromIds = _.pluck _.where(msgs, from : login), 'msgId' 

,需要過濾由「到」還有:

toIds = _.pluck _.where(msgs, to : login), 'msgId' 

這是行不通的,因爲to是一個數組。我如何過濾to?如果有人幫助我,我將不勝感激!

回答

3

此時您需要使用_.filter。如果你看看source code,你可以看到_.where只是圍繞_.filter提供的有用包裝。 _.where適合基於原始比較的簡單過濾,但任何更復雜的工作都需要自己編寫。

# Filter for messages that contain the target address. 
matchedTo = _.filter msgs, (msg) -> _.contains msg.to, login 

# Pluck as usual 
toIds = _.pluck matchedTo, 'msgId'