2

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)

3
  • I think you should introduce the id property in predef list 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) Commented Nov 28, 2018 at 7:24
  • okay cool ! thats for the if else thing, anything more can be done for orderingOfTabs Commented Nov 28, 2018 at 7:30
  • i have an update to the post, also i am not able to add properties to the predefinedList Commented Nov 28, 2018 at 7:39

4 Answers 4

1

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; }

Sign up to request clarification or add additional context in comments.

1 Comment

i have updated the question. Can you check the userAccessArray and if else condition have a change in the question. Can you please update the answer
0

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)

4 Comments

thanks vivek :) for orderOfTabs, can i reduce the finalTabsArray
but i have an update to the post , can you check the post
Regarding orderOfTabs, there isn't much optimization be done. Best thing you can do is to ensure predefinedList comes in the order that you want.
okay , i have updated the question. Can you check the userAccessArray and if else condition have a change in the question. Can you please update your answer
0
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)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.