我正在尝试为我的 jsgrid 创建一个搜索引擎,但我遇到了一个问题。如何搜索大写和小写文本?
因为现在,只有使用相同的符号才能得到结果。
loadData: function (filter) {
var ajaxItemsFiltered = [];
$.each(ajaxItems, function (index, value) {
var allow = true;
$.each(value, function (index2, value2) {
if (typeof filter[index2] !== "undefined" && filter[index2] !== '') {
if (value2.indexOf(filter[index2]) === -1) {
allow = false;
}
}
});
if (allow) {
ajaxItemsFiltered.push(value);
}
});
return ajaxItemsFiltered;
},
最佳答案
您可以使用 Intl.Collator 来执行不区分大小写的比较
const localeCompare = Intl.Collator('en', { sensitivity: 'base' }).compare;
function includesByIgnoreCase(predicate) {
if (this.filter(predicate).length > 0) {
return true;
}
return false;
};
const value2 = ['hElLo', 'world', 'foo', 'Hello Wolrd'];
console.log(
includesByIgnoreCase.call(
value2,
e => localeCompare(e, 'hello world') !== 0
)
);
引用资料: MDN:Intl.Collator.prototype.compare
关于javascript - 大小写搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55304208/