Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
How to keep previous object active using ngFor in angular?

I have the json data as below.

 [
{
    "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
}
 ]

I am trying to loop through each object and if title is same , Iam displaying only the first object in the menubar. Based on the isActive field (which gets updated based on route) I am applying the active class to the object.

The following are the 3 titles displayed in the menubar and I want to keep the driver active for both the driver objects.

  • Vehicle
  • Driver
  • Quote

I am comparing the current title with previous title and if both matches , i am skipping that object using ngIf condition

  <ul>
    <ng-container let menu of menuArray; let i=index>
      <!-- for 0 index there is no title , so ignore -->
      <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>

How to keep the first driver object active when the current index is in second driver object (The second driver object will be skipped in the ngIf condition and not displayed in menubar)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If I understand correctly, I think you just want to place the first active one, so compare if the last one was active

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

EDIT

Just noticed this is very much the same as @farincz recommendation in the comments.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!