Tips for maintaining the current object's status while utilizing ngFor in Angular

The JSON data provided is structured as follows.

 [
{
    "route":"vehicle",
    "next-route":"driver",
    "isActive":false
},
{
    "title":"Driver",
    "route":"driver",
    "next-route":"driver-details",
    "isActive":false
},
{
    "title":"Driver",
    "route":"driver-details",
    "next-route":"quote",
    "isActive":true
},
{
    "title":"Quote",
    "route":"quote",
    "next-route":"",
    "isActive":false
}
 ]

An attempt is being made to iterate through each object. If the title matches, only the first object in the menu bar is displayed. Based on the 'isActive' field (which changes according to the route), the active class is applied to the object.

The following are the titles displayed in the menu bar and it is desired for both driver objects to remain active:

  • Vehicle
  • Driver
  • Quote

A comparison is made between the current and previous title. If they match, that object is skipped using an ngIf condition.

  <ul>
    <ng-container let menu of menuArray; let i=index>
      <!-- For index 0, there is no title, so it should be ignored -->
      <ng-container *ngIf=" menu && menu.title && menuArray[i-1].title !== menuArray[i].title">
        <li [ngClass]="{active: menu.isActive}">
          <div>{{menu.title}}</div>
        </li>
      </ng-container>
    </ng-container>
  </ul>

The goal is to keep the first driver object active when the current index is within the second driver object (the second driver object will be skipped in the ngIf condition and not displayed in the menu bar).

Answer №1

From my understanding, it seems like you are looking to place the first active one by comparing if the last one was active

<ng-container *ngIf=" menu && menu.title && (menuArray[i-1].title !== menuArray[i].title || !menuArray[i-1].isActive)">

UPDATE

Upon further review, I realized that this is quite similar to the suggestion made by @farincz in the comments.

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

Using setInterval within componentDidUpdate

https://i.sstatic.net/Si1Up.png https://i.sstatic.net/6KUIL.png Hey there! I have a scenario in my app.js where, upon meeting a specific condition, I switch the value of my toPing state from false to true. This change is then checked in componentDidUpdate ...

Is it possible to use data retrieved from an API in Angular Url Matcher to determine if the matcher should be considered true or not based on the API response?

The following question was posed during a micro-learning video session focused on Custom Angular URL Matchers in the Spartacus Storefront Is it possible to extract prefix data from an API response and determine whether a matcher is true based on that dat ...

"Utilize Cypress to simulate clicking a button by triggering the enter key

Currently, I'm conducting testing on my application and attempting to trigger a button click using the Enter key: <v-btn class="submit-button" block color="primary" @click="login" > Log In < ...

Using Electron to redirect to a different HTML file while passing GET variables

I am currently working on developing an Electron app. Within the project files, for example, I have app.html. My goal is to send GET variables like ?id=54&q=asd to the receiver.html, where I can then retrieve these id and q variables. As we all know, ...

What is the best way to ensure that navigation takes up the full width of the screen in pixels when

Trying to use jQuery to set a specific pixel width for my Bootstrap navbar that spans the entire width of the browser window. However, encountering some bugs as the width needs to be recalculated whenever the browser is resized. Currently, I have this cod ...

Activate night mode in ElementPlus using CDN

Is there a way to set the theme to dark in ElementUI + Vue when using CDNs instead of npm? <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0" /> ...

Error message: The Node.js filtered LS command is missing a ")" after the argument list

I've been working on the learnyounode workshop and I'm stuck on a code issue. After running it through jslint, I received this feedback: Expected ')' to match '(' from line 6 but instead saw '{'. Oddly enough, line ...

I am experiencing an issue with my React app deployed on Heroku where it successfully loads the home page but fails

My react application performs perfectly when run locally and is successfully deployed on heroku. However, upon clicking any link from the home page to another page, a blank page with 'not found' message appears. No other error messages are displa ...

Is Consistency Possible with CSS Transition Direction?

Take a look at this example: https://codepen.io/jon424/pen/XWzGNLe In the provided code snippet, there is a button that can toggle the visibility of an image. When clicked, the image disappears from bottom to top and reappears when clicked again. The use ...

Ways to cite a vendor in static class functions?

Is there a method to access a service provided at bootstrap within static class methods? I don't have any code to share right now, but I've been experimenting with the standard syntax using Injector (you can find more information here). Whenever ...

Compare and contrast JSON files that are constantly changing using JavaScript

I have reviewed the various inquiries on comparing JSON objects and feel confident in my ability to implement a comparison. However, my question pertains to dynamically loading source .json files into JSON objects for comparison. The scope of my project h ...

Cannot chain promises using 'then'

Having trouble understanding why the 'describeDir' promise chain is not working properly. Can anyone help me figure out what I did wrong here? Everything in the code seems to run, but functions like then or finally from the promise API never get ...

Invoke a JSP page using JavaScript

Hello, I'm new to web development and I have a question about calling JSP from a JavaScript file. My project consists of an html file with JavaScript (home.html) and a JSP file (login.jsp). In the home.html file, there are 2 textboxes and 2 buttons - ...

The error message "The function 'combineLatest' is not found on the Observable type"

Hey there! I'm currently working on implementing an InstantSearch function into my website using Angular 12.2. To accomplish this, I'll be working with a Firestore database with the index "book-data". In my search component, I have included the f ...

X-ray Picker failing to retrieve a value

I am currently using the most recent version of the x-ray npm module in the code snippet below. Despite my expectations, the Meta and Metatags elements remain empty when I print out obj. Can anyone help me identify what I might be missing? var Xray = req ...

Vue components failing to reflect code changes and update accordingly

Initially, the component functions properly, but subsequent changes require me to restart the server in order to see any updates. ...

The discord.js argument for startsWith should not be a standard regular expression

https://i.sstatic.net/UG79z.png What could be the reason behind this not working as expected? I am trying to check if a string starts with a number, and furthermore, I want it to handle multiple numbers. For instance, if the string starts with 1259823 the ...

Automatically collapse the responsive menu upon selecting a menu item

I have implemented a custom JavaScript code for a basic open/close menu movement on multi-page websites. The code works by closing the menu only when the user clicks the menu symbol. However, I now need to incorporate this code into a one-page website and ...

Issue with Parsley validation not functioning as expected

I have implemented parsley.js to validate my form, but I am facing an issue when trying to validate a checkbox using the data-mincheck attribute. Below is the snippet of my code: <form data-validate="parsley"> <p>Please choose at least 2 ...

Can you explain the contrast between the @HostBinding() directive and ElementRef/Renderer in Angular?

I'm currently in the process of developing a directive for a dropdown toggle feature. Through my research, I have come across two different approaches to implement this directive. Which method would be considered the most effective practice? Approach ...