Home > Mobile >  How do i filter out category and header and get the index of array of filtered tabs?
How do i filter out category and header and get the index of array of filtered tabs?

Time:06-22

Below are the given input

const category = "Western Food"
const header = "Lamb chops"

Below is the array that needs to be filtered

const data = [
  {
    category: "Western Food",
    tabs: [
      {
        header: "Pork chops"
      },  
      {
        header: "Lamb chops"
      }, 
    ]
  }
]

Output (Index of Lamb chops)

Output = 1 

CodePudding user response:

const getTabIndex=(selectedCategory,selectedHeader)=>{
const selectedTab=data.find(category=> category===selectedCategory)

if(selectedTab===undefined) return -1

return selectedTab.findIndex(tab=>tab.header===selectedHeader)

}

This function should return -1 when no matching values found.

  • Related