3

I have two arrays: books and booksDisplay. In my template I use *ngFor to iterate through my booksDisplay array:

<div *ngFor="let bookDisplay of booksDisplay">
   <div class="book">
      <div class="title" *ngIf="???">{{ ???.title }}
   </div>
</div>

But inside my *ngFor-Element I need to look up in my books-Array, where id is equal to the current bookId in my booksDisplay-Array. So how can I use something like a where clause inside my *ngFor-Element?

4
  • You could create a dictionary/lookup property in your component where the key is the book id and reference that inside the *ngFor block. Or you can join the 2 arrays into one array and iterate over that. Commented Jul 20, 2018 at 14:02
  • In my opinion, you should use method in the component.ts to filter those books then look up those books in the UI. Let the UI do the simple render will increase your performance. Commented Jul 20, 2018 at 14:05
  • could you please set an example in stackblitz.com/edit/angular-xcgx1h Commented Jul 20, 2018 at 14:18
  • 2
    Add your array's structure. Commented Jul 20, 2018 at 14:18

3 Answers 3

2

Something like this should work. However, I would better prepare data in a model instead of making such calculations in the template..

<div *ngFor="let bookDisplay of booksDisplay>
   <div class="book">
      <ng-container *ngIf="checkIfExist(bookDisplay.id)">
        <div class="title">{{ books[bookDisplay.id] }}
      </ng-container>
   </div>
</div>

Template:

checkIfExist(id: number) {
  return this.books.find( book => book['id'] === id ) 
}
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming your component.ts looks something like this:

export class AppComponent {
   booksDisplay = [
    {
      id: 1,
      title: 'a'
    },
    {
      id: 2,
      title: 'b'
    },
    {
      id: 3,
      title: 'c'
    },
  ]
}

You can a counter to ngFor and use that value to filter on with ngIf conditional like so:

<div *ngFor="let book of booksDisplay; let i= index">
  <div class="book">
    <div class="title" *ngIf="i === book.id">{{ book.title }}
    </div>
    <p>{{i}}</p>
  </div>

2 Comments

Thanks. But it does not work for me because the index isn't the book id. One book has more than one element in the booksDisplay array.... Is there another way to solve this?
I feel like I'm misunderstanding your question. Can you provide a small sample of the data that you are trying to sift through? I can edit my answer accordingly.
0

Like this; https://angular.io/guide/displaying-data

<ng-container *ngFor="let book of booksDisplay; let i = index"> 
   <ng-container *ngIf="book.id === '1'">  
   <p> {{book.description}}</p>
</ng-container> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.