CallumVass
CallumVass

Reputation: 11458

Knockout Array Filter on more than 1 Observable

I have an observableArray which I pass through the arrayFilter to return a filtered set. But I'm only doing it for 1 observable at the moment. How can I add a second observable:

return ko.utils.arrayFilter(self.complaints(), function (complaint) {
    return complaint.RaisedBy == self.currentUser();
});

So I also want to filter based on the Code property of my complaint object:

return ko.utils.arrayFilter(self.complaints(), function (complaint) {
    return complaint.Code == self.currentCode();
});

How can I combine them two?

Upvotes: 0

Views: 1703

Answers (2)

BonyT
BonyT

Reputation: 10940

Something like this should work:

self.myDependantObservable = ko.computed(function(){
   var currentUser = self.currentUser();
   var currentCode = self.currentCode();

   return result = ko.utils.arrayFilter(self.complaints(), function (complaint) {
     return (complaint.RaisedBy == currentUser && complaint.Code == currentCode);
   });

});

Upvotes: 0

mjbates7
mjbates7

Reputation: 674

have you tried:

return ko.utils.arrayFilter(self.complaints(), function (complaint) {
    return complaint.Code == self.currentCode() && complaint.RaisedBy == self.currentUser();
});

Upvotes: 2

Related Questions