gpt4 book ai didi

backbone.js - Backbone - findWhere : How to find a model by querying for a nested object

转载 作者:行者123 更新时间:2023-12-04 23:21:30 26 4
gpt4 key购买 nike

这是我收藏的样本集:

{
"name": "Bob",
"class": "3",
"marks": {
"maths": 70,
"science": 85
}
},
{
"name": "Ron",
"class": "3",
"marks": {
"maths": 80,
"science": 90
}
}

使用 Backbone 的 findWhere,我可以根据这样的查询获得模型:
Coln.findWhere({"name": "Ron"});

但是如何根据数学标记查询模型?下面的代码似乎不起作用:
Coln.findWhere({"marks.maths": 80});

最佳答案

收藏有各种Underscore methods混入其中。特别是有 find 它允许您指定谓词函数,以便您可以执行以下操作:

Coln.find(function(m) { return m.get('marks').maths === 80 })

如果要查找所有匹配的模型,请使用 filter .

关于backbone.js - Backbone - findWhere : How to find a model by querying for a nested object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25027783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com