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

自定义过滤功能未按预期工作,请求技术排查协助

Troubleshooting Your Multi-Property Filter Function

Hey there! Let’s break down why your custom filter might not be behaving as expected, based on the code snippet you shared. I’ve spotted a few common pitfalls and fixes to get it working right:

Potential Issues & Fixes

  • Uninitialized entities handling
    If this.entities is undefined or null initially, assigning it to array will cause a Cannot read property 'filter' of undefined error. Always add a fallback to an empty array:

    let array = this.entities || [];
    
  • Empty filter values are not skipped
    If a filter key has an empty string value (after trimming), your current logic will still run the filter—but includes('') returns true for every string. This means empty filters won’t narrow results at all, which might not be what you want. Add a check to skip empty filter conditions:

    Object.keys(filter).forEach(key => {
      const filterString = filter[key].trim().toLowerCase();
      if (!filterString) return; // Skip empty filter values
      // Rest of your filtering logic
    });
    
  • Debugging propertyAccessor reliability
    If this.propertyAccessor isn’t correctly retrieving the entity’s property value, your filter will never work as intended. Add a quick debug log to verify the values being compared:

    const propertyValue = this.propertyAccessor(entity, key);
    const propertyString = (propertyValue || '').toString().trim().toLowerCase();
    console.log(`Comparing ${key}: entity value = "${propertyString}", filter value = "${filterString}"`);
    
  • MobX createTransformer dependency tracking
    Since you’re using createTransformer (from MobX), make sure this.entities is an observable array. If it’s a plain array, the transformer won’t re-run when the data changes, leading to stale results.

Revised Code Example

Here’s a polished version of your function incorporating these fixes:

public filtered = createTransformer((filter: { [key: string]: string }): EntityType[] => {
  let array = this.entities || []; // Fallback to empty array

  Object.keys(filter).forEach(key => {
    const filterString = filter[key].trim().toLowerCase();
    if (!filterString) return; // Skip empty filters

    array = array.filter(entity => {
      const propertyValue = this.propertyAccessor(entity, key);
      const propertyString = (propertyValue || '').toString().trim().toLowerCase();
      return propertyString.includes(filterString);
    });
  });

  return array;
});

Debugging Tips

  • Log the initial array value before filtering to confirm you’re starting with the correct dataset.
  • Check the filter object to ensure it’s receiving the key-value pairs you expect (e.g., no typos in property names).
  • Test with a single filter key first to isolate whether the issue is with multi-property logic or a specific property’s handling.

内容的提问来源于stack exchange,提问作者Kamil Łapiński

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:00:24