backbone.js使用子字符串过滤集合

backbone.js filter collection using substring

本文关键字:字符串 过滤 集合 js backbone      更新时间:2023-09-26

下面的问题让我抓狂。

_.each(collection, function( account, key ){
    var totalPhy = that.physicianCollection.where({ 'Hospital_Id__c' : account.Id }).length;
    account.physicians = { 'total' : totalPhy };
});

Hospital_Id__c与帐户相同时,它正在工作。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 };

http://underscorejs.org/#filter