简体   繁体   中英

backbone.js filter collection using substring

The following problem is driving me crazy.

_.each(collection, function( account, key ){

    var totalPhy = that.physicianCollection.where({ 'Hospital_Id__c' : account.Id }).length;
    account.physicians = { 'total' : totalPhy };
});

It is working when Hospital_Id__c same as account.Id. But my account Id is a sub string of the hospital_Id__c . How do I search and get the count? I tried index of and search methods. Pls suggest. Thanks in advance.

_.where is a simple use case of _.filter to match exact properties. In your case you will need to actually use _.filter and write the logic yourself. I'm not sure what the account id / hospital id look like, but the code will probably look something like :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM