I possess an assortment of objects and I must retrieve specific information about them based on two data points

Managing a collection of items can be challenging, especially when filtering based on specific data points. For example, let's say we have a collection of action objects like {name: Detail}, {name: Spec}... and two data points determining which actions to use based on category and page. How can we best implement an OOP approach to efficiently handle this problem, particularly as the scale increases to hundreds of actions across ten categories and pages? Previous attempts using the Factory pattern have not been successful.

Answer №1

To simplify the process, a mapping of pages and their corresponding allowed actions can be created. For instance:

const actions = [{name: 'View'}, {name: 'Edit'}];

const pages = [
   { 
     name: 'Home',
     allowedActions: [{name: 'View'}]
   },
   { 
     name: 'Profile',
     allowedActions: [{name: 'Edit'}]
   }
]

const getAllowedActions = pageName => {
  return pages.find(page => page.name == pageName).allowedActions;
}

console.log(getAllowedActions('Home'));

Output:

[ { name: 'View' } ]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it advisable to include auto-generated files in an npm package upon publication?

I have a TypeScript NPM package where my build process compiles all *.ts files into myLib.d.ts, myLib.js, and myLib.js.map. In order for my NPM package to function properly, it requires the src/*.ts files as well as the auto-generated myLib.* files. Howe ...

Tap here to switch between 2 jquery functions

Due to the deprecation of the toggle() method in jQuery version 1.8 and its removal in version 1.9, an alternative approach is needed for versions 1.11 and above. You can check out the documentation for more information. If you are looking to replicate th ...

Handling Datatable: Executing an asynchronous function post an Ajax request

Upon receiving data from the web server, I want to manipulate it in Datatable. However, the response data is encoded and requires decoding using an asynchronous function called decodeData(). I attempted to call this async function in the dataSrc section as ...

Numerical order in dropdown menu

There are 3 select dropdowns and I want the following scenarios to occur: If I select 1 in dropdown1, then the other values should be 2 and 3. Dropdown1: 1 Dropdown2: 2 Dropdown3: 3 https://i.sstatic.net/078H4.png If I select 1 in dropdown2, then t ...

Tips for moving a div away from the mouse cursor

Creating a landing page where I need my logo to move away from the mouse cursor in a specific direction, but it's currently moving randomly. The page is built using HTML and I have the flexibility to utilize any open-source JavaScript library. <! ...

Using Typescript to iterate through nested interface data with the `forEach()` method

CountTargetsData serves as an interface for parsing JSON data with multiple levels of nesting, as illustrated below. interface CountTargetsData { data: { [state: string]: { [date: string]: { [index: string]: { [id: string]: { [targets: ...

Possible revision: "Methods for updating Bootstrap language settings?"

Currently, I am working on a project using ASP.NET Core in combination with Bootstrap. Recently, I successfully integrated localization support by following the guidance provided in this documentation. However, I encountered an issue where the Bootstrap fr ...

Retrieving MongoDB Query Data in Node API - What is the best way to send the mongoDB query results back to the

As a newcomer to JavaScript, I decided to challenge myself by diving into Node API development, specifically experimenting with querying my MongoDB. In my API's index.js script utilizing the 'express' module, I aim to return query results in ...

Distinguishing Between Global and Local Variables

let picture = document.getElementById('image1'); function moveLeftArrow () { picture.style.left = parseInt(picture.style.left) - 5 + 'px'; } This code is designed to allow users to move images using the arrow keys. Interestingly, th ...

Utilize a custom enum from a separate file to extend and redefine the i18next TFunction

I am attempting to customize the TFunction from the i18next package. The goal is to enforce typing on the i18n keys being used as follows: t('invalid-key') // should be invalid t('key1') // should be valid To achieve this, I created a ...

Does NextJS come with an index.html template page pre-installed?

Today, I delved into NextJS for the first time as I transitioned a ReactJS website over to it. While I find it to be a powerful framework, there is one particular feature that seems to be missing. In traditional ReactJS (without the NextJS framework), we ...

The useEffect function is not being executed

Seeking assistance from anyone willing to help. Thank you in advance. While working on a project, I encountered an issue. My useEffect function is not being called as expected. Despite trying different dependencies, I have been unable to resolve the issue ...

What is the importance of using mutations, setters, and getters for effectively managing state?

As I delve into the world of state management in Vue.js, I am finding it to be quite complex and confusing with all the different methods such as mutations, getters, and setters. Why can't we just change data directly? Wouldn't that make the code ...

Input field with ruled lines beneath each line of text

Can you assist in creating a textarea that displays lines under each row of text similar to the example shown in the image below? ...

Is it feasible to arrange <DIV>s based on their dates?

I encountered an issue while working with an ordering function. var $wrapper = $('#list'); $wrapper.find('.blogboxes').sort(function (a, b) { return +b.dataset.date - +a.dataset.date; }) .appendTo( $wrapper ); <script src="ht ...

A program designed to access and retrieve a universal variable

It seems like a simple task, but I can't seem to figure it out. I'm trying to assign the currentUserId within the getCurrentUser function so that I can use it in other functions. Currently, the code below is returning undefined. What am I overl ...

Is there a way to view the type signature of the resulting intersection type (type C = A & B) in IDE hints, rather than just seeing the components?

When analyzing types defined by intersection in Typescript, I notice that the hint remains identical to the original definition: https://i.stack.imgur.com/mjvI8.png However, what I actually want is to visualize the resulting shape, similar to this: http ...

Executing PHP code from a list item

On one of my website pages, I have a list that functions as a delete button. However, I am interested in making it so that when a user clicks on the delete option, a php script is triggered - similar to how a submit button works. Below is the list structu ...

The concealed field is not being established due to the Ajax AutoCompleteExtender OnClientItemSelected functionality

Currently, I am attempting to utilize the Ajax AutoCompleteExtender provided below: <asp:TextBox runat="server" Width="300" ID="tbxItem" CssClass="NormalUpper" /> <asp:AutoCompleteExtender ID="autoCompleteExtenderItemName" runat="server" Mini ...

How to efficiently register services for multiple instances in Angular

Currently, my service includes a field that represents a ViewContainerRef and a method to set the value of this field. @Injectable({ providedIn: 'root' }) export class SomeService { public viewContainerRef: ViewContainerRef setViewContaine ...