使用DRY方法创建动态过滤逻辑

huangapple go评论52阅读模式
英文:

Creating dynamic filter logic using DRY approach

问题

I want to create a reusable filter function that works by passing in three properties

  1. The users input
  2. The data type
  3. The data field that is being queried

So far I have created the following code

  const [filteredData, setFilteredData] = useState("");
  const [filterValue, setFilterValue] = useState("");
  
  const handleFilter = (e, type, field) => {
    setFilterValue(e)
    if (type === 'text') {
      const filterLowerCase = e.toLowerCase().trim()
      const result = props.searchResults.filter(d => 
        !filterLowerCase ||  d[field].toLowerCase().includes(filterLowerCase)
      );
      setFilteredData(result);
    }
  }

The issue is I don't know how to dynamically set the 'field' property.
The code works when the 'field' property is replaced with the actual field name, in the example below this is 'task_name'.

  const handleFilter = (e, type, field) => {
    setFilterValue(e)
    if (type === 'text') {
      const filterLowerCase = e.toLowerCase().trim()
      const result = props.searchResults.filter(d => 
        !filterLowerCase ||  d.task_name.toLowerCase().includes(filterLowerCase)
      );
      setFilteredData(result);
    }
  }

How can this code be written so that the field can be dynamically set.

英文:

I want to create a reusable filter function that works by passing in three properties

  1. The users input
  2. The data type
  3. The data field that is being queried

So far I have created the following code

  const [filteredData, setFilteredData] = useState("");
  const [filterValue, setFilterValue] = useState("");
  
  const handleFilter = (e,type,field) => {
    setFilterValue(e)
    if (type === 'text') {
      const filterLowerCase = e.toLowerCase().trim()
      const result = props.searchResults.filter(d => 
      !filterLowerCase ||  d.field.toLowerCase().includes(filterLowerCase),
      );
      setFilteredData(result);
    }
  }

The issue is I don't know how to dynamically set the 'field' property.
The code works when the 'field' property is replaced with the actual field name, in the example below this is 'task_name' .

  const handleFilter = (e,type,field) => {
    setFilterValue(e)
    if (type === 'text') {
      const filterLowerCase = e.toLowerCase().trim()
      const result = props.searchResults.filter(d => 
      !filterLowerCase ||  d.task_name.toLowerCase().includes(filterLowerCase),
      );
      setFilteredData(result);
    }
  }

How can this code be written so that the field can be dynamically set.

答案1

得分: 0

你可以使用方括号表示法,就像这样:

  const [filteredData, setFilteredData] = useState("");
  const [filterValue, setFilterValue] = useState("");
  
  const handleFilter = (e, type, field) => {
    setFilterValue(e)
    if (type === 'text') {
      const filterLowerCase = e.toLowerCase().trim()
      const result = props.searchResults.filter(d => 
      !filterLowerCase ||  d[field].toLowerCase().includes(filterLowerCase),
      );
      setFilteredData(result);
    }
  }
英文:

You can use bracket notation like this:

  const [filteredData, setFilteredData] = useState("");
  const [filterValue, setFilterValue] = useState("");
  
  const handleFilter = (e,type,field) => {
    setFilterValue(e)
    if (type === 'text') {
      const filterLowerCase = e.toLowerCase().trim()
      const result = props.searchResults.filter(d => 
      !filterLowerCase ||  d[field].toLowerCase().includes(filterLowerCase),
      );
      setFilteredData(result);
    }
  }

huangapple
  • 本文由 发表于 2023年6月8日 08:23:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定