// 模糊匹配
fuzzyMatch(str, key){
? ? let index = -1, flag = false;
? ? for(var i = 0, arr = key.split(""); i < arr.length; i++ ){
? ? ? ? //有一個關(guān)鍵字都沒匹配到,則沒有匹配到數(shù)據(jù)
? ? ? ? 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:行行"}]