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

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

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

  1. const [filteredData, setFilteredData] = useState("");
  2. const [filterValue, setFilterValue] = useState("");
  3. const handleFilter = (e, type, field) => {
  4. setFilterValue(e)
  5. if (type === 'text') {
  6. const filterLowerCase = e.toLowerCase().trim()
  7. const result = props.searchResults.filter(d =>
  8. !filterLowerCase || d[field].toLowerCase().includes(filterLowerCase)
  9. );
  10. setFilteredData(result);
  11. }
  12. }

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'.

  1. const handleFilter = (e, type, field) => {
  2. setFilterValue(e)
  3. if (type === 'text') {
  4. const filterLowerCase = e.toLowerCase().trim()
  5. const result = props.searchResults.filter(d =>
  6. !filterLowerCase || d.task_name.toLowerCase().includes(filterLowerCase)
  7. );
  8. setFilteredData(result);
  9. }
  10. }

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

  1. const [filteredData, setFilteredData] = useState("");
  2. const [filterValue, setFilterValue] = useState("");
  3. const handleFilter = (e,type,field) => {
  4. setFilterValue(e)
  5. if (type === 'text') {
  6. const filterLowerCase = e.toLowerCase().trim()
  7. const result = props.searchResults.filter(d =>
  8. !filterLowerCase || d.field.toLowerCase().includes(filterLowerCase),
  9. );
  10. setFilteredData(result);
  11. }
  12. }

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' .

  1. const handleFilter = (e,type,field) => {
  2. setFilterValue(e)
  3. if (type === 'text') {
  4. const filterLowerCase = e.toLowerCase().trim()
  5. const result = props.searchResults.filter(d =>
  6. !filterLowerCase || d.task_name.toLowerCase().includes(filterLowerCase),
  7. );
  8. setFilteredData(result);
  9. }
  10. }

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

答案1

得分: 0

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

  1. const [filteredData, setFilteredData] = useState("");
  2. const [filterValue, setFilterValue] = useState("");
  3. const handleFilter = (e, type, field) => {
  4. setFilterValue(e)
  5. if (type === 'text') {
  6. const filterLowerCase = e.toLowerCase().trim()
  7. const result = props.searchResults.filter(d =>
  8. !filterLowerCase || d[field].toLowerCase().includes(filterLowerCase),
  9. );
  10. setFilteredData(result);
  11. }
  12. }
英文:

You can use bracket notation like this:

  1. const [filteredData, setFilteredData] = useState("");
  2. const [filterValue, setFilterValue] = useState("");
  3. const handleFilter = (e,type,field) => {
  4. setFilterValue(e)
  5. if (type === 'text') {
  6. const filterLowerCase = e.toLowerCase().trim()
  7. const result = props.searchResults.filter(d =>
  8. !filterLowerCase || d[field].toLowerCase().includes(filterLowerCase),
  9. );
  10. setFilteredData(result);
  11. }
  12. }

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:

确定