下面的问题让我抓狂。
_.each(collection, function( account, key ){
var totalPhy = that.physicianCollection.where({ 'Hospital_Id__c' : account.Id }).length;
account.physicians = { 'total' : totalPhy };
});
当 Hospital_Id__c
与 account.Id 相同时,它就可以工作。但我的帐户 ID 是 hospital_Id__c
的子字符串。如何搜索并获取计数?我尝试了索引和搜索方法。请建议。提前致谢。
最佳答案
_.where
是 _.filter
的一个简单用例,用于匹配精确的属性。在您的情况下,您需要实际使用 _.filter
并自己编写逻辑。我不确定帐户 ID/医院 ID 是什么样子,但代码可能类似于:
var totalPhy = that.physicianCollection.filter(function(phys, index, collection){
//phys is your model
return phys.get('Hospital_Id__c').indexOf(account.Id) != -1;
//(or however the ids are set, your logic here)
}).length;
account.physicians = { 'total' : totalPhy };
关于javascript - backbone.js 使用子字符串过滤集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26208901/