以下将创建filtered
包含id为 inbatches
的元素。通过使用下划线或类似的东西可以大大简化它。boxes
var boxes = ko.observableArray([{id:1},{id:3}]);
var batches = ko.observableArray([{bId:1, name:"ID1"},{bId:2, name:"ID2"},{bId:3, name:"ID3"},{bId:4, name:"ID4"}]);
var filtered = ko.utils.arrayFilter(batches(), function (batch) {
for (var i = 0; i < boxes().length; i++)
if (batch.bId == boxes()[i].id)
return true;
return false;
});
工作小提琴http://jsfiddle.net/JD2Q2
所以在你的例子中它会像
var filtered = ko.utils.arrayFilter(self.batches(), function (batch) {
for (var i = 0; i < visibleCheckBoxes().length; i++)
if (batch.bId == visibleCheckBoxes()[i].id)
return true;
return false;
});