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)
}
]
I tried this solution that is working and achieving what i want
<div class="message" *ngFor="let user of users | slice:0:1;">
But I know this is looping through the array, I need to get the first element without doing it.
I already tried this :
<span> {{ users[0].event }} </span>
it's throwing me an error cause sometimes the event is undefined
users[0]?.eventdivorspanconditional to the existence ofusers[0], which leads me to the conclusion that you want to centralize it in a methodfirstUser, see below.