我是js的新手,我需要限制搜索栏以仅在用户输入3个字符后才显示我的值,我这样做是因为可能分配了可能返回的值,因此限制为3个字符。
const function = () => {
if (customerDatas) {
let data = [];
if (searchBar != "") {
data = customerDatas.filter(customerData => {
return customerData.name.toLowerCase().includes(searchBar.toLowerCase());
});
}
return data;
}
};
答案 0 :(得分:1)
在过滤之前检查搜索字符串的长度,searchBar.length >= 3
const function = () => {
if (customerDatas) {
let data = [];
if (searchBar != "" && searchBar.length >= 3) {
data = customerDatas.filter(customerData => {
return customerData.name.toLowerCase().includes(searchBar.toLowerCase());
});
}
return data;
}
};
答案 1 :(得分:-1)
我相信searchBar是一个字符串,因此您可以使用string.length
if (searchBar != "" && searchBar.length >= 3) {
data = customerDatas.filter(customerData => {
return customerData.name.toLowerCase().includes(searchBar.toLowerCase());
});
}