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.