0

I am using the traditional ngFor for iterating across an array followed with index, my sample code:

<div *ngFor="let object of Objects; let i = index">

I want to know is it possible to get the last index from the array in i? I know I can find the first or any other by if i === 1, etc, but is there a method to check directly if i is the last one from the array? Thanks.

0

3 Answers 3

6
<div *ngFor="let object of Objects; let i = index; first as isFirst; last as isLast">

isFirst and isLast will be booleans.

But then again, the documentation explains it !

Sign up to request clarification or add additional context in comments.

Comments

1
<div *ngFor="let object of Objects; let i = index; first as isFirst; last as isLast"> 
<div *ngIf="first">{{object}}</div>
</div>

1 Comment

i! While this may answer OP's question, code only answers are generally discouraged on SO. It is more beneficial for OP and future visitors to add an explanation to code provided. Thanks! --From Review
0

I think its already in there, you can use

{{Objects.length-1}}

to get the last index of your array

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.