You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular搜索筛选无法同时匹配product_name和product_content如何解决

修复方案

问题根因

你代码中使用了JavaScript的逗号运算符,该运算符会按顺序执行括号内的所有表达式,但最终仅返回最后一个表达式的结果。因此product_name的匹配逻辑完全被丢弃,只有最后一句product_content的匹配结果生效。

修复代码

将逗号替换为逻辑或运算符||即可实现「两个字段任意一个匹配就返回结果」的需求,同时建议提前把搜索文本转小写,避免大小写匹配异常:

return products.filter((items) => {
  // 统一转小写避免大小写匹配失败
  const search = searchText.toLowerCase();
  return (
    items.product_name.toLowerCase().includes(search) ||
    items.product_content.toLowerCase().includes(search)
  );
});

补充说明

如果你后续需求调整为「两个字段同时匹配才返回结果」,把||替换为逻辑与运算符&&即可。

内容的提问来源于stack exchange,提问作者Smart Code

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 14:36:03