我正在尝试使用本地存储对我的应用程序实现自动完成。 有没有办法使用“LIKE %value%”条件在单词数组中进行搜索?
var autocompleteArr = ['two', 'three', 'twenty two', 'twelve'];
mySearchMethod(autocompleteArr, 'tw'); //['two', 'twenty two', 'twelve']
最佳答案
如果您对部分搜索感兴趣,请使用 filter :
function match(value) {
return value.match(/.*tw.*/);
}
var filtered = ['two', 'three', 'twenty two', 'twelve'].filter(match);
// filtered is ['two', 'twenty two', 'twelve']
如果对精确匹配感兴趣,那么 indexOf
就可以了。
关于javascript - 在数组内搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31747514/