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

时间:2014-10-06 01:15:23

标签: javascript xcode backbone.js underscore.js

以下问题让我发疯。

_.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的子字符串。我如何搜索并获得计数?我试过索引和搜索方法。请建议。提前谢谢。

1 个答案:

答案 0 :(得分:1)

_.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