有这么一个数组 [{a,'123',b:'345',c:'456',d:'t12'},{a,'234',b:'345',c:'thf2',d:'t12'}],
现在希望能够通过逗号分隔搜索值的输入方式,从数组中过滤出模糊匹配的数组元素。
function filterByInput(array, input) {
// 解析输入,如果输入有逗号,就将其分割为多个子字符串,然后分别进行过滤
const searchValues = input.split(',').map(value => value.trim());
return array.filter(item => {
for (let searchValue of searchValues) {
// 对数组中的每个元素进行过滤,如果元素的值包含搜索值,则返回 true
if(Object.values(item).toString().toLocaleLowerCase().indexOf(searchValue.toLocaleLowerCase())>-1){
return true
}
}
// 如果数组中的元素的值都不包含搜索值,则返回 false
return false;
});
}
// 测试代码
const array = [{a:'123',b:'345',c:'456',d:'t12'},{a:'234',b:'345',c:'thf2',d:'t12'}];
const input = '123,thf2'; // 你可以修改这个输入值进行测试
console.log(filterByInput(array, input)); // 输出过滤后的数组
同理,你可以使用其他分隔符,如 &
。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有