how to get a model by array params from a collection
I have a model like this:
model = from: "firstname.lastname@example.org" id: 1 to: [email@example.com]
and I have a collection containing these kind of models. The collection needs to filter by from. I know _.where an underscore.js function. I am using it like this:
fromIds = _.pluck _.where(msgs, from : login), 'msgId'
and need to filter by 'to' as well:
toIds = _.pluck _.where(msgs, to : login), 'msgId'
it does not work, because to is an array. How can I filter by to ? I would be grateful if someone helped me out !
At that point you need to use _.filter. If you look at the source code you can see that _.where is just a helpful wrapper around _.filter. _.where is good for simple filtering based on primitive comparison, but anything more complex you will have to write yourself.
# Filter for messages that contain the target address. matchedTo = _.filter msgs, (msg) -> _.contains msg.to, login # Pluck as usual toIds = _.pluck matchedTo, 'msgId'