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.