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

React如何实现数组多字段搜索(过滤)?

Multi-Field Array Search & Fixing Pagination Conflict

Hey there! Let's work through your problems step by step—first getting that multi-field search working in a concise way, then fixing the pagination conflict so you don't have to comment out code.

Your current code only checks the title field, but we can expand this to target multiple fields (like title, date, id, name) in a clean, single-line filter. Here are two solid approaches:

If you want to limit searches to only predefined fields (avoids matching unintended data), use an array of field names with the some() method:

filterList = (e) => {
  const { value } = e.target;
  this.setState({ value }, () => {
    const searchTerm = value.toLowerCase();
    // Single-line filter for specific fields
    const updatedList = this.state.issues.filter(item => 
      ['title', 'date', 'id', 'name'].some(field => 
        String(item[field]).toLowerCase().includes(searchTerm)
      )
    );
    this.setState({ issues: updatedList });
  });
}
  • some() returns true if any of the specified fields matches the search term, keeping the item in the filtered list.
  • String() ensures non-string values (like numeric ids or date objects) are converted safely to strings, preventing runtime errors.
  • includes() is more readable than search() for simple substring matching here.

Option 2: Search All Fields Automatically

If you want to check every field in the item object without listing them explicitly:

filterList = (e) => {
  const { value } = e.target;
  this.setState({ value }, () => {
    const searchTerm = value.toLowerCase();
    const updatedList = this.state.issues.filter(item => 
      Object.values(item).some(val => 
        String(val).toLowerCase().includes(searchTerm)
      )
    );
    this.setState({ issues: updatedList });
  });
}
  • Object.values(item) extracts all values from the item into an array, then we check if any value contains the search term.

2. Fixing the Pagination Conflict

The reason you had to comment out the page validation is because your setPage method was using the original this.props.items instead of the filtered this.state.issues. Here's the fixed version:

setPage(page) {
  const filteredItems = this.state.issues; // Use filtered search results instead of original list
  const { pageSize } = this.props;
  let pager = this.state.pager;

  // Recalculate total pages based on the filtered list
  const totalPages = Math.ceil(filteredItems.length / pageSize) || 1;
  // Restore the validation check now that it uses the correct total pages
  if (page < 1 || page > totalPages) {
    return;
  }

  pager = this.getPager(filteredItems.length, page, pageSize);
  const pageOfItems = filteredItems.slice(pager.startIndex, pager.endIndex + 1);
  this.setState({ pager: pager });
  this.props.onChangePage(pageOfItems);
}
  • Now all pagination logic uses the filtered issues state instead of the unfiltered original list.
  • The page validation check works correctly again because it references the total pages of your search results, not the full unfiltered dataset.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:53:11