不知情的情况下帮别人做网站他违法,建设银行信用卡境外网站盗刷,紫金网站建设价格,手机网址大全哪个好场景#xff1a;
有这么一个数组 [{a,123,b:345,c:456,d:t12},{a,234,b:345,c:thf2,d:t12}], 现在希望能够通过逗号分隔搜索值的输入方式#xff0c;从数组中过滤出模糊匹配的数组元素。
解析#xff1a;
可以使用 JavaScript 的 filter 函数和 indexOf 函数来实现这个功…场景
有这么一个数组 [{a,123,b:345,c:456,d:t12},{a,234,b:345,c:thf2,d:t12}], 现在希望能够通过逗号分隔搜索值的输入方式从数组中过滤出模糊匹配的数组元素。
解析
可以使用 JavaScript 的 filter 函数和 indexOf 函数来实现这个功能。
源码
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)); // 输出过滤后的数组同理你可以使用其他分隔符如 。