Skip to content
Advertisement

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

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 

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement