filter+indexof实现模糊搜索
//根据搜索框数据过滤出结果
//value是搜索框onChange事件传递过来的值,options是数据(对象数组的形式)
searchInfoChange = value => {
const {options} = this.props;
//首先把输入框里面的英文转换为小写
const lowerCaseValue = value?.toLowerCase();
//通过filter去找到每一项,然后取出里面label值转换为小写,然后再把lowerCaseValue传入查找
//最后把找到的结果重新通过seState进行赋值
const searchResult = options.filter(item => (item as any)?.label?.toLowerCase()?.indexOf(lowerCaseValue) >= 0);
this.setState({
optionList: searchResult,
});
};
filter+includes实现模糊搜索
const searchResult = options.filter(item => (item as any)?.label?.toLowerCase()?.includes(lowerCaseValue));
filter+startsWith实现从起始位置开始匹配
const searchResult = options.filter(item => (item as any)?.label?.toLowerCase()?.startsWith(lowerCaseValue));
filter+'==='实现精准匹配
const searchResult = options.filter(item => (item as any)?.label?.toLowerCase()===lowerCaseValue);