// 模糊匹配
fuzzyMatch(str, key){
let index = -1, flag = false;
for(var i = 0, arr = key.split(""); i < arr.length; i++ ){
//有一个关键字都没匹配到,则没有匹配到数据
if(str.indexOf(arr[i]) < 0){
break;
}else{
let match = str.matchAll(arr[i]);
let next = match.next();
while (!next.done){
if(next.value.index > index){
index = next.value.index;
if(i === arr.length - 1){
flag = true
}
break;
}
next = match.next();
}
}
}
return flag
},
//使用方法
let list = [{
id:1,
name:‘x行行’
},{
id:2,
name:‘小熊’
}
]
let searchVal = ‘行行’
let resultArr= list.filter(item => {
if(fuzzyMatch(item.name.trim(), searchVal)){
return item
}
})//返回[{id:1,name:行行"}]