So I have an array of userAccessArray, where each user has what all things can access based on that array i am checking with predefinedArrayList where all the objects comes for the application and creating a new array of objects. [Filtering it]
after that i am rearranging the order based on another array. Thats my final result.
Below is the code, its working but i thought like there should some more better way.
let predefinedList = [{name: "Home Page", path:"/home"},{name: "About Page", path:"/about"}, {name: "Edit Page", path:"/edit"}, {name: "Admin Page", path:"/admin"} ]
let userAccessArray = ["editing", "aboutUs", "home"]
let userAccessList = userAccessArray.map(userAccess => {
if(userAccess === "aboutUs"){
return predefinedList[1]
}else if(userAccess === "editing"){
return predefinedList[2]
}else if(userAccess === "home"){
return predefinedList[0]
}else if(userAccess === "adminAccess"){
return predefinedList[3]
}
})
const orderOfTabs = ["Home Page", "Edit Page", "About Page", "Admin Page"]
const finalTabsArray = orderOfTabs.map(orderOfTab => userAccessList.find(userAccess => userAccess.name === orderOfTab)).filter(item => item)
console.log("finalTabsArray", finalTabsArray)
let predefinedList = [{name: "Home Page", path:"/home",id: 'home'}...and orderofTabs and userAccessArray based on id. it will make filtering easy.let userAccessList = predefinedList.filter(route=>userAccessArray.indexOf(route.id) !== -1)