LisaN
LisaN

Reputation: 165

Display only the first item of object array

I have an array that i want to display from it the first item in a span .

At the moment i'm getting all the value instead of only the first one.

  <div class="card">  
    <div *ngIf="selectedUser._id">
      <div class="user" *ngFor="let user of users">
             <span>  {{ user.event }} </span>
            </div>
  </div>
  </div>

It's returning me the list of all users date event, i only want the first one

I also tried to add user.event[0] not working it's displaying me the first char of the date

my array of object

[{id:XYZ, event:Fri Jul 20 2018 15:00:04 GMT+0200 (CEST)},{id:XYZ1, name:Fri Jul 10 2018 15:00:04 GMT+0200 (CEST)},{id:XYZ2, name:Fri Aug 20 2018 15:00:04 GMT+0200 (CEST)}]

Upvotes: 1

Views: 169

Answers (4)

Sajeetharan
Sajeetharan

Reputation: 222582

You dont need

<div class="card">  
    <div>
      <div class="user">
             <span>    {{ users[0].event | date:'medium' }} </span>
     </div>
</div>  

STACKBLITZ DEMO

Upvotes: 0

lakshitha madushan
lakshitha madushan

Reputation: 669

<div class="card">
  <div>
    <div class="user">
      <div>{{users[0]}}</div>
    </div>
  </div>
</div> 

Upvotes: 0

Krishna Rathore
Krishna Rathore

Reputation: 9687

you can use slice pipe.

<div class="user" *ngFor="let user of users | slice:0:1;">
    <span>  {{ user.event }} </span>
</div>

Upvotes: 0

Malindu Sandaruwan
Malindu Sandaruwan

Reputation: 1517

Use the simple approch below,

<div class="card">  
    <div *ngIf="selectedUser._id">
      <div class="user">
         <span>  {{ users[0].event }} </span>
      </div>
    </div>
</div>

Upvotes: 1

Related Questions