简体   繁体   English

如何动态过滤而不是使用 if-else 语句

[英]How to filter dynamically instead of using if-else statements

I have a redux hook responsible for server data that I want to pass into a set state after filtering it.. But with a dynamic condition.我有一个 redux 挂钩负责服务器数据,我想在过滤后将其传递到一组 state 中。但具有动态条件。

// states 
const [drilledData, setDrilledData] = useState([])

Current Code:当前代码:

const drillDownData = (seriesIndex, dataPointIndex) => {
    // here series index could be from 1-80 in that order
    // dataPointIndex is a number and item.dataSet is also a number
    // Refactor this code to remove if-else statements to something better

    if (seriesIndex === 0) {
      const filteredData = coscoData.filter((item) => item.dataSet === dataPointIndex)
      setDrilledData(filteredData[0].shipments)
    } else if (seriesIndex === 1) {
      const filteredData = hapagData.filter((item) => item.dataSet === dataPointIndex)
      setDrilledData(filteredData[0].shipments)
    } else if (seriesIndex === 2) {
      const filteredData = maerskData.filter((item) => item.dataSet === dataPointIndex)
      setDrilledData(filteredData[0].shipments)
    } else if (seriesIndex === 3) {
      const filteredData = othersData.filter((item) => item.dataSet === dataPointIndex)
      setDrilledData(filteredData[0].shipments)
    } else {
      setDrilledData([])
    }
    return null
  }

Currently, the if-else statement is straightforward and works fine.目前,if-else 语句简单明了并且工作正常。 But I have to repeat that if else statement 80 times.但我必须重复 if else 语句 80 次。 because I have to filter the data and with respect to series index and dataPointIndex and its related dataSet to set them conditionally因为我必须过滤数据并针对系列索引和 dataPointIndex 及其相关的 dataSet 有条件地设置它们

like data example像数据示例

aclData,
admiralLineData,
anlData,
aplData,
arkasData,

data inside them is like that but dataSet can b more than one.其中的数据是这样的,但 dataSet 可以是多个。 so I'm using the filter to pick up matching dataSet to the pointIndex.所以我使用过滤器来获取与pointIndex匹配的数据集。

aclData = ['dataSet': 1, 'shipments' : [...{more objects}]]

So, I want to make that if else statement dynamic so I don't have to write up to 80 times to cover all the dataSets.所以,我想让 if else 语句动态化,这样我就不必写多达 80 次来覆盖所有数据集。 How I can do that.我怎么能做到这一点。 ? ? I don't need to you refactor all code accurately, any idea or pseudo code will also be very helpful.我不需要你准确地重构所有代码,任何想法或伪代码也会很有帮助。

Is the relationship between the index number and the relevant data set static?是索引号和相关数据集static的关系吗? If so, could you create a util object that records those relationships and then use that to dynamically call the data set that you are trying to use?如果是这样,您能否创建一个工具 object 来记录这些关系,然后使用它来动态调用您尝试使用的数据集? It would be less clunky than a giant if else (or switch statement), and reusable.它比巨型 if else (或 switch 语句)更笨重,并且可重用。

If the pattern for the above suggestion is too tricky to implement, a switch statement that calls a helper function, can at least reduce your code:如果上述建议的模式难以实现,那么调用帮助程序 function 的 switch 语句至少可以减少您的代码:

const filterMyData = (pertinentData, dataIndex) => {
    const filteredData = pertinentData.filter((item) => item.dataSet === 
    dataIndex)
    setDrilledData(filteredData[0].shipments)
}

and then接着

  switch (seriesIndex) {
     case 0:
       filterMyData(coscoData, dataToPointIndex);
       break;
     case 1:
       filterMyData(hapagData, dataToPointIndex);
       break;
     ....
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM