Javascript 搜索子字符串敲除js
我试图通过数组进行筛选,并检查数组中是否有任何标题包含用户正在搜索的内容。但是,当我运行该函数时,它似乎只检查搜索是否与标题完全匹配,而不检查是否包含子字符串 JSJavascript 搜索子字符串敲除js,javascript,html,knockout.js,data-binding,Javascript,Html,Knockout.js,Data Binding,我试图通过数组进行筛选,并检查数组中是否有任何标题包含用户正在搜索的内容。但是,当我运行该函数时,它似乎只检查搜索是否与标题完全匹配,而不检查是否包含子字符串 JS var viewModel = function() { var self = this; self.filter = ko.observable(''); self.locationList = ko.observableArray(model); self.filterList = function(){ return
var viewModel = function() {
var self = this;
self.filter = ko.observable('');
self.locationList = ko.observableArray(model);
self.filterList = function(){
return ko.utils.arrayFilter(model, function(location) {
if(self.filter().includes(location.title)){
console.log(location.title)
}
});
};
}
HTML
<div class="col-lg-12">
<div class="input-group">
<input data-bind="textInput: filter, event:{keyup: filterList}"
type="text" class="form-control" placeholder="Filter Places"
aria-describedby="basic-addon2" id="test">
<button id="basic-addon2">Filter</button>
</div>
</div>
滤器
如果(self.filter().includes(location.title)){
这里您要检查搜索的键是否包含数组中的一个标题。相反,您应该检查其他项
if(location.title.includes(self.filter())){
您可以使用简单的.filter
和.includes
进行此操作
var filterdList = self.locationList.filter(e=> e.includes(self.filter()))
这就是我刚才所做的,它似乎在起作用,谢谢你的回复。很高兴知道这一点。如果它解决了你的问题,请考虑接受和投票。