javascript - 搜索子字符串淘汰js
问题描述
我正在尝试过滤一个数组并检查我的数组中的任何标题是否包含用户正在搜索的内容。但是,当我运行该函数时,它似乎只检查搜索是否与标题完全匹配,而不是是否包含子字符串。
JS
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>
解决方案
问题出在这部分if(self.filter().includes(location.title)){
在这里,您正在检查搜索的键是否包含数组中的标题之一。相反,您应该另外检查
if(location.title.includes(self.filter())){
你可以使用简单.filter
的.includes
var filterdList = self.locationList.filter(e=> e.includes(self.filter()))
推荐阅读
- excel - 使用 VBA 清除不连续单元格内容的最快方法
- r - r 数据框检查下 n 个变量
- php - 从一个刀片传递值,但不一定从其他刀片传递
- apache-kafka - 如何使用命令行创建 Kafka 消费者组?
- r - 基于列向量的匹配
- javascript - 在 react-table-2 的同一列中显示名字和姓氏
- angular - 主从角度材质示例
- node.js - 如何使用 cPanel 在 GoDaddy VPS 上部署 Node JS 应用程序?一步一步的过程将受到高度赞赏
- hibernate - Hibernate:当没有物理列或值用于区分实例时,鉴别器列如何工作?
- jquery - 如何对数据表进行排序(bDestroy)并添加合并单元格功能