如何从一个集合度日阵列PARAMS模型 [英] how to get a model by array params from a collection
问题描述
我有这样一个模型:
model =
from: "a@b.com"
id: 1
to: [c@d.com]
和我有一个包含这几样模型的集合。收集需要通过从
过滤。我知道 _。其中,
的 underscore.js
功能。我使用它是这样的:
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 !
推荐答案
在这一点上,你需要使用 _。过滤
。如果你看一下源$ C $ C 你可以看到该 _。其中,
就位于 _一个有用的包装。过滤
。 _。其中,
是基于原始的比较好简单的过滤,但任何更复杂,你将不得不自己写。
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'
这篇关于如何从一个集合度日阵列PARAMS模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!