0

I am using Javascript filter function for filtering the array. Array containing the objects related to Covid cases. I am using Rapid API for fetching the world covid cases details.

import axios from 'axios'

export const stats_API = async (setWorldStats, setcasesAcrossWorld) => {
  try {
    const url = `https://covid-193.p.rapidapi.com/statistics`

    const headers = {
      'x-rapidapi-host': 'covid-193.p.rapidapi.com',
      'x-rapidapi-key': '******************',
    }
    const { data } = await axios.get(url, { headers })

    let casesAcrossWorld_Arr = data.response.filter((val) => {
      return val.country === 'All'
    })

    setcasesAcrossWorld(casesAcrossWorld_Arr[0])

Filtering worldStats_Arr on the basis of All & ASIA

    let worldStats_Arr = data.response.filter((val) => {
      return val.country !== 'All' && val.country !== 'Asia'
    })

Now second time again but with different conditions on Filtering worldStats_Arr on the basis of EUROPE & AFRICA

    worldStats_Arr = worldStats_Arr.filter((val) => {
      return val.country !== 'Europe' && val.country !== 'Africa'
    })

Now again filtering & so on...


    worldStats_Arr = worldStats_Arr.filter((val) => {
      return val.country !== 'North-America' && val.country !== 'South-America'
    })

    worldStats_Arr = worldStats_Arr.filter((val) => {
      return val.country !== 'Oceania' && val.country !== 'Africa'
    })

    setWorldStats(worldStats_Arr)
  } catch (error) {
    console.log(error)
  }
}

As you will see I am filtering worldStats_Arr again again so that I can fullfill multiple filters condition.

Is there any other way or a good practice? How can I filter array on the basis of multiple filtering scenarios.

Akhil Rana
  • 145
  • 2
  • 12
  • 5
    How are your multiple filters represented? You have shown us hard-coded values... which you could have put in *one* filter with just more `&&` operators, or using `!["Europe", "Asia"].includes(val.country)` – trincot Sep 15 '21 at 19:46
  • 1
    You could make a function that takes an array of countries you'd like to filter out. Then run a check whether the value is in that array as a filter condition. – fynmnx Sep 15 '21 at 19:47
  • 1
    Instead of doing `worldStats_Arr = worldStats_Arr.filter(...)` you can just chain the `.filter` like `worldStats_Arr = worldStats_Arr.filter().filter().filter()` – programmerRaj Sep 15 '21 at 19:48
  • 1
    Does this answer your question? [Array filtering with multiple conditions javascript](https://stackoverflow.com/questions/52520048/array-filtering-with-multiple-conditions-javascript) – fynmnx Sep 15 '21 at 19:50
  • 1
    You can store your conditions in an array and then use either `reduce()` or `every()` as shown in the top answer here: [Good way to chain filter functions in javascript](https://stackoverflow.com/questions/48834275/good-way-to-chain-filter-functions-in-javascript) – AndrewL64 Sep 15 '21 at 19:50
  • All solutions & suggestions are working well. Thanks to all of you – Akhil Rana Sep 16 '21 at 04:27

0 Answers0