Reputation: 9125
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)
Upvotes: 2
Views: 79
Reputation: 16
How about changing the predefinedList and then have easy access:
let predefinedList = {
"home": {name: "Home Page", path:"/home"},
"aboutUs": {name: "About Page", path:"/about"},
"editing": {name: "Edit Page", path:"/edit"},
"admin": {name: "Admin Page", path:"/admin"}
}
let userAccessArray = ["editing", "aboutUs", "home"];
let userAccessList = userAccessArray.map(item => predefinedList[item]);
console.log(userAccessList);
EDIT: changed the code based on your changes in the code. As for the orderOfTabs, this can't be really optimized.
Upvotes: 0
Reputation: 2375
let predefinedList = [{name: "Home Page", path:"/home"},{name: "About Page", path:"/about"}, {name: "Edit Page", path:"/edit"}, {name: "Admin Page", path:"/admin"} ]
const orderOfTabs = ["Home Page", "Edit Page", "About Page", "Admin Page"];
let userAccessArray = ["admin", "edit", "about", "home"]
function getUserActionList(type) {
switch(type) {
case 'about':
return predefinedList[1];
case 'home':
return predefinedList[0];
case 'edit':
return predefinedList[2];
case 'admin':
return predefinedList[3];
}
}
let userAccessList = userAccessArray.map(userAccess => getUserActionList(userAccess));
userAccessList.sort( function (a, b) {
var prevV = a['name'], nextV = b['name'];
return (orderOfTabs.indexOf(prevV) > orderOfTabs.indexOf(nextV)) ? 1 : -1;
});
console.log(userAccessList)
Upvotes: 0
Reputation: 386680
I suggest to use an access
property for filtering predefinedList
and an object for sorting the items with a default value for unknown name
properties. In this case, this items are sorted at the end of the list by taking a huge value Infinity
.
const
orderOfTabs = { "Home Page": 1, "Edit Page": 2, "About Page": 3, "Admin Page": 4, default: Infinity },
predefinedList = [{ name: "Home Page", path:"/home", access: "home" }, { name: "About Page", path:"/about", access: "aboutUs" }, { name: "Edit Page", path:"/edit", access: "editing" }, { name: "Admin Page", path:"/admin", access: "adminAccess" }],
userAccessArray = ["editing", "aboutUs", "home"],
finalTabsArray = predefinedList
.filter(({ access }) => userAccessArray.includes(access))
.sort(({ name: a }, { name: b }) =>
(orderOfTabs[a] || orderOfTabs.default) - (orderOfTabs[b] || orderOfTabs.default));
console.log(finalTabsArray);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 1
Reputation: 2755
Instead of using an if..elseif
ladder, you can use the .filter
method to generate the userAccessList
array. See the code below.
let predefinedList = [{
name: "Home Page",
path: "/home"
}, {
name: "About Page",
path: "/about"
}, {
name: "Edit Page",
path: "/edit"
}, {
name: "Admin Page",
path: "/admin"
}]
let userAccessArray = ["edit", "about", "home"]
let userAccessList = predefinedList.filter(item =>
userAccessArray.indexOf(item.path.substr(1)) > -1)
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)
Upvotes: 0