Filtering nested arrays in Angular by cross-referencing with a navigation menu

In the legacy application I'm working on, we have a navigation menu along with a list of user roles. Due to its legacy nature, we have accumulated a significant number of user roles over time.

The main goal is to dynamically display the navigation menu based on the roles assigned to each user.

  • Keep in mind that a single user can have multiple roles and a particular menu item can be accessible by more than one role.
  • To achieve this, we need to compare the user's roles with the roles specified for each menu item and its child items.

Below are the arrays containing the User Role information and Navigation Items data:

The objective here is to efficiently filter down the navigation items array based on the user's roles.

User Role Array:

["ABS","ADM", ... (roles listed) ..., "USER"]

Nav Item array:

(Navigation items interface defined here)

The solution I've tried so far hasn't delivered the desired outcome. It seems like I may have made a mistake somewhere as it's not functioning as intended.

(Code snippet attempting to match user roles with navigation items)

If anyone has any suggestions or insights on how to approach this issue, your help would be greatly appreciated! :)

Answer №1

I approached this problem differently by utilizing the reduce function and a couple of some methods. My assumption was that if the target role was not found in the main navigation, then it should skip the children. This function takes an array of roles as input and filters down the navigation accordingly.

const getNavForRole = (roles) => {
  return navItems.reduce((b, a) => {
    if (!a.role.some(r => roles.includes(r))) return b;
    a.children = a.children.filter(f => f.role.some(r => roles.includes(r)))
    return [...b, a]
  }, [])
}

let navItems = [{
    // code snippet continues...

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

What is the process for configuring my CSS and JS files to send HTTP response headers?

During our security evaluation of a web application built in Laravel 4, we discovered that the Anti-MIME-Sniffing header X-Content-Type-Options was not properly configured to 'nosniff'. The following PHP code snippet sets the necessary HTTP heade ...

Problem with Ajax functionality on Jquery Dialog

I have implemented a Jquery.dialog feature in my application for sending messages. When the user clicks on "new", the dialog box appears, allowing them to select the recipient (currently working with IDs instead of usernames). On the first opening of the ...

Change the CSS menu item to directly load the website instead of using javascript to replace a placeholder

I have a CSS navigation menu, but I'm facing an issue. When I click on a link in the menu, like Google for example, it only changes the name of the placeholder and doesn't actually load the page. Any suggestions on how to fix this? Thank you for ...

It appears that the mysqli_query function is executing more than once

One of the features on my website involves displaying different URLs inside an iframe. To ensure that each URL is only shown once to the user, I have implemented a custom function. The process involves creating arrays of available URLs and previously seen ...

Issue with form validation causing state value to remain stagnant

Giving my code a closer look, here is a snippet in HTML: <input type="text" name="email" id="email" autoComplete="email" onChange={(e) => {validateField(e.target)}} className="mt-1 ...

Utilizing ASCX to trigger JavaScript functions within an ASPX page

I created a user control named MyListSelect.ascx that contains a list of radio buttons... <%@ Control Language="C#" %> <select> <option disabled selected>Select Options</option> <option> option 1 </opti ...

Retrieve the value from an input tag that is only displayed based on a condition using *ngIf

In my HTML form, I have implemented conditional visibility of input fields based on the radio button selection using *ngIf. <table> <tr> <td><label>name</label></td> <td><input #name />&l ...

Issues arise with loading AngularJS directive properly within the context of service utilization

Currently, I am diving into the world of JavaScript and tackling a project that involves fetching data from MongoDB. My task is to write code in AngularJS to create a pie chart using Highcharts. Surprisingly, everything works smoothly when I solely use an ...

Tips on generating model names dynamically within a service

I am currently utilizing a service to assign unique numbers to model names. The resulting data structure is as follows: "sectionInputs": [ { "model": "price_min1" }, { "model": "price_max2" }, { "model": "units_occ3" }, { "m ...

Using JQuery to enable the movement of divs on a screen through clicking and dragging, without the use of

Recently, I've been experimenting with a small project involving draggable divs. However, the code I've written doesn't seem to be functioning properly, causing JQuery to become unresponsive. Is there an alternative method that is simple an ...

Choose not to alter the display value during keyup and keydown events, while still triggering the change event

$(function(){ $(".cls_user_details select").keyup(function(){ $(".cls_user_details select").val("Profile"); }) }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="cls_user_setti ...

What could be causing the ajax request to not go through?

Check out this function I created that triggers an event when any inputs in an HTML form are changed. Function Snippet: function customEvent(form, element) { var timer; $(element).keyup(function () { clearTimeout(timer); if ($(ele ...

Creating one-to-one relationships in sequelize-typescript can be achieved by setting up multiple foreign keys

I have a question about adding multiple foreign keys to an object. Specifically, I have a scenario with Complaints that involve 2 Transports. One is used to track goods being sent back, and the other is used for goods being resent to the customer. @Table({ ...

Tips for Uploading Large Images to Backend API in Next.js

As I work on building my portfolio using NextJS, I encountered an issue with the Project Functionality. When converting images to base64 and sending them to an API for uploading on Cloudinary, everything runs smoothly as long as the total size of the req ...

Determining Asynchrony in Node.js Through Programming

Is there a way to assess if a function can be executed asynchronously without requiring a callback? I am currently working with Node.js on the Intel Edison platform and utilizing mraa. The native C++ functions like i2c.readReg(address) do not have provisi ...

Strategies for Obtaining a Response Following a Function Execution in React JS

Hello, I am encountering an issue where I am calling a function and trying to get a response, but it is returning undefined. The response is being passed from the parent component to the child component. Here is the code for the component: import React fr ...

Utilize jQuery and JSP (or PHP) to showcase detailed information about a specific item when a user clicks on it within an HTML page

For my upcoming college project, I am tasked with developing a movie library. The main page of the library will feature various movie posters. Upon clicking on a poster, users will be directed to a dedicated page displaying detailed information about the ...

What is the best way to ensure that decimal numbers are shown in the right locale number format by default in an Ionic/Angular app

I am currently working on an Ionic/Angular application that frequently requires the display of decimal numbers on its pages. The LOCALE_ID in my app is set to "cs", so I expect numbers to be displayed using this locale. Ideally, "1,000.23" would display as ...

Ionic 2 hides the form input area within its input component

I set up a login page, but the input fields are not showing up on the form. Here is my current code: <ion-list> <ion-item> <ion-label fixed>Username</ion-label> <ion-i ...

Issue encountered when attempting to invoke a PHP function using Javascript through AJAX

I'm currently working on incorporating a PHP function into my HTML file using Ajax (which also contains JavaScript). My goal is to retrieve all locally stored JSON files. I've been following a guide at , but I'm facing some challenges. When ...