我在js中实现了一个搜索,核心思想是我有一个搜索词数组,让它是
const arr = ['i', 'cant', 'solve', 'this', 'problem', 'alone']
和我有一个搜索输入值(可以是一个或两个字,或更多),让它是
const string = 'i cant swim'
这个输入不会给我任何结果,因为实际上我们在数组中没有连续的这三个单词。但是如果我们取
const string = 'i cant'
应该返回['i', 'cant']
作为搜索数组中连续的2个单词。我如何实现它?我的尝试如下:
const search = []
const searchStringArr = string.split(' ') // ['i', 'cant']
searchStringArr.forEach(str => {
search.push(arr.filter(word => word.toLowerCase().includes(str.toLowerCase())))
})
但是它返回找到的所有单词的数组,而不考虑它们在搜索数组中的顺序。有什么好办法吗?
这是一个简单的方法。如果arr包含这两个,您还需要匹配['can't' 'swim']吗?(即不仅从字符串的开始匹配)?
这里有一个解决方案:
function findConsecutiveMatches(string, arr){
const searchStringArr = string.split(' '); // ['i', 'cant', 'swim']
let search = [];
arr.forEach((el, idx) => {
if (searchStringArr[0].toLowerCase() != arr[idx].toLowerCase()){
return;
}
search.push(el);
if (idx == arr.length -1){
return;
}
for (let diff = 1; diff <= Math.min(arr.length - idx + 1, searchStringArr.length -1); diff++){
if (searchStringArr[diff].toLowerCase() != arr[idx + diff].toLowerCase()){
return;
}
search.push(arr[idx + diff]);
}
});
if (search.length != searchStringArr.length){
search = [];
}
console.log(search);
return search;
}
const arr0 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
const string0 = "I cant";
findConsecutiveMatches(string0, arr0);
// output: ['i', 'cant'];
const arr1 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
const string1 = "I cant swim";
findConsecutiveMatches(string1, arr1);
// output: [];
const arr2 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
const string2 = "solve this";
findConsecutiveMatches(string2, arr2);
// output: ['solve', 'this'];
const arr3 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
const string3 = "solve this question";
findConsecutiveMatches(string3, arr3);
// output: [];
const arr4 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
const string4 = "I solve this";
findConsecutiveMatches(string4, arr4);
// output: [];
输出示例:
const arr0 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
const string0 = "I cant";
findConsecutiveMatches(string0, arr0);
// output: ['i', 'cant'];
const arr1 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
const string1 = "I cant swim";
findConsecutiveMatches(string1, arr1);
// output: [];
const arr2 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
const string2 = "solve this";
findConsecutiveMatches(string2, arr2);
// output: ['solve', 'this'];
const arr3 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
const string3 = "solve this question";
findConsecutiveMatches(string3, arr3);
// output: [];
const arr4 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
const string4 = "I solve this";
findConsecutiveMatches(string4, arr4);
// output: [];
您可能想要查看一些有用的数组方法。
const search = []
const searchStringArr = string.split(' ') // ['i', 'cant']
searchStringArr.forEach(str => {
if (arr.some(item => item.toLowerCase() === str.toLowerCase())) search.push(str)
})
你应该将你的搜索字符串分割成一个数组(基于空格分隔符),然后将所有单词与你的数组进行比较。
const arr = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
const search = 'i cant swim';
var wordsArray = search.split(" ");
var result = [];
wordsArray.forEach( searchWord => {
if(arr.includes(searchWord)) {
result.push(searchWord);
}
});
console.log( result );