I have the following array data
var data = [
{name:'hr', to:'/hr/settings', children:[
{name:'Language', to:'/hr/settings/general/languages', icon:''},
{name:'Marital Status', to:'/hr/settings/general/marital-status', icon:''},
{name:'Nationalities', to:'/hr/settings/general/nationalities', icon:''},
{name:'Ethnicities', to:'/hr/settings/general/ethnicities', icon:''},
{name:'Religions', to:'/hr/settings/general/religions', icon:''},
{name:'Tribes', to:'/hr/settings/general/tribes', icon:''},
{name:'Relations', to:'/hr/settings/general/relations', icon:''}
]},
{name:'education', to:'/hr/education', children:[
{name:'Universities',to:'/hr/settings/education/universities', icon:''},
{name:'Relations',to:'//hr/settings/education/relations', icon:''}
]}
];
So what am looking forward to do is find the index of the data array which has to value in it or its children similar to a certain string
so i have the following
function getArrayIndex(tovalue) {
const pathnavigated = data.find(link => link.to === tovalue);
return data.indexOf(pathnavigated);
}
The above works for the array eg getArrayIndex('/hr/settings') but now i want it to search also the children
eg
getArrayIndex('/hr/settings/general/marital-status')
should return the first index
and getArrayIndex('/hr/settings/education/universities') should return the second index
How do i make the function to search even the children and return the value of the index on the parent array (of the child)