Home:ALL Converter>Filtering the array and reordering the array based on another array

Filtering the array and reordering the array based on another array

Ask Time:2018-11-28T15:14:11         Author:Learner

Json Formatter

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)

Author:Learner,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53514019/filtering-the-array-and-reordering-the-array-based-on-another-array
Nina Scholz :

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.\n\n\r\n\r\nconst\r\n orderOfTabs = { \"Home Page\": 1, \"Edit Page\": 2, \"About Page\": 3, \"Admin Page\": 4, default: Infinity },\r\n 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\" }],\r\n userAccessArray = [\"editing\", \"aboutUs\", \"home\"],\r\n finalTabsArray = predefinedList\r\n .filter(({ access }) => userAccessArray.includes(access))\r\n .sort(({ name: a }, { name: b }) =>\r\n (orderOfTabs[a] || orderOfTabs.default) - (orderOfTabs[b] || orderOfTabs.default));\r\n\r\nconsole.log(finalTabsArray);\r\n.as-console-wrapper { max-height: 100% !important; top: 0; }",
2018-11-28T07:41:00
Vivek :

Instead of using an if..elseif ladder, you can use the .filter method to generate the userAccessList array. See the code below.\n\n\r\n\r\nlet predefinedList = [{\r\n name: \"Home Page\",\r\n path: \"/home\"\r\n}, {\r\n name: \"About Page\",\r\n path: \"/about\"\r\n}, {\r\n name: \"Edit Page\",\r\n path: \"/edit\"\r\n}, {\r\n name: \"Admin Page\",\r\n path: \"/admin\"\r\n}]\r\n\r\nlet userAccessArray = [\"edit\", \"about\", \"home\"]\r\n\r\n\r\nlet userAccessList = predefinedList.filter(item =>\r\n userAccessArray.indexOf(item.path.substr(1)) > -1)\r\n\r\n\r\nconst orderOfTabs = [\"Home Page\", \"Edit Page\", \"About Page\", \"Admin Page\"]\r\n\r\nconst finalTabsArray = orderOfTabs.map(orderOfTab => userAccessList.find(userAccess => userAccess.name === orderOfTab)).filter(item => item)\r\n\r\nconsole.log(\"finalTabsArray\", finalTabsArray)",
2018-11-28T07:26:24
Sagar Jajoriya :

let predefinedList = [{name: \"Home Page\", path:\"/home\"},{name: \"About Page\", path:\"/about\"}, {name: \"Edit Page\", path:\"/edit\"}, {name: \"Admin Page\", path:\"/admin\"} ]\nconst orderOfTabs = [\"Home Page\", \"Edit Page\", \"About Page\", \"Admin Page\"];\n\nlet userAccessArray = [\"admin\", \"edit\", \"about\", \"home\"]\n\nfunction getUserActionList(type) {\n switch(type) {\n case 'about':\n return predefinedList[1];\n case 'home':\n return predefinedList[0];\n case 'edit':\n return predefinedList[2];\n case 'admin':\n return predefinedList[3];\n }\n}\n\nlet userAccessList = userAccessArray.map(userAccess => getUserActionList(userAccess));\n\nuserAccessList.sort( function (a, b) {\n var prevV = a['name'], nextV = b['name'];\n return (orderOfTabs.indexOf(prevV) > orderOfTabs.indexOf(nextV)) ? 1 : -1;\n});\n\nconsole.log(userAccessList)\n",
2018-11-28T07:49:42
yy