1. const getInf = (str, key) => str.replace(new RegExp(`${key}`, 'g'), `%%${key}%%`).split('%%');
2.封裝對應(yīng)的方法
data 為需要匹配的數(shù)據(jù)??keyWord為關(guān)鍵字??seachName為數(shù)據(jù)中對應(yīng)的對象名
seachRedText(data,keyWord,seachName){//查找關(guān)鍵字
? ? let that = this
? ? ? ? ? ? ? for (let i = 0; i < data.length; i++) {
? ? ? ? ? ? ? ? ? let dic = data[i];
? ? ? ? ? ? ? ? ? let newDic = data[i];
? ? ? ? ? ? ? ? ? let text = dic[seachName];
? ? ? ? ? ? ? ? ? newDic[seachName] = getInf(text,keyWord);
? ? ? ? ? ? ? }
? ? ? return data
? }
3.使用??res.data.rescueList = this.seachRedText(res.data.rescueList,this.data.keyWord,'name')
4.案例
5.效果