我已将以下过滤器添加到我的视图模型中:
contactsViewModel.filterItems = ko.computed(function () {
var filter = this.filterInput().toLowerCase();
if (!filter || !this.hasEnoughChars()) {
// Simply keep the results if input is empty or less 3 chars are in it
return this.contacts();
} else {
// iterate through the JSON again
return ko.utils.arrayFilter(this.contacts(), function (item) {
// Return the objects that contain the filter term
// ko.utils.stringContains is not part of knockout
// Manually added to knockout-2.2.0.debug.js line 238
return ko.utils.stringContains(item.firstName().toLowerCase(), filter)
|| ko.utils.stringContains(item.lastName().toLowerCase(), filter)
|| ko.utils.stringContains(item.email().toLowerCase(), filter)
|| ko.utils.stringContains(item.company().toLowerCase(), filter);
});
}
}, contactsViewModel);
如何通过减少调用 stringContains 的次数来改进此代码?我希望能够写:
return ko.utils.stringContains(item.FirstName().toLowerCase() || item.lastName().toLowerCase() || item.email().toLowerCase() || item.company().toLowerCase(), filter);
但是当我这样做时,stringContains 没有返回正确的值
从淘汰赛开始:
stringContains: function (string, contains) {
string = string || "";
return string.indexOf(contains) != -1;
}
非常感谢您的帮助