How can I write a filter , which only returns the last element of a list / how can I write a filter which returns all but the last element.
Background: I'm having a list of items, where the last item should be editable. Therefor I need this filter to get it.
You could use a Pipe
to filter an array
pipe
@Pipe({name: 'lastItem'})
export class LastItemPipe implements PipeTransform {
transform(items: any[], exclusive: boolean): any {
if (exclusive) { return items.slice(0, items.length - 1); }
return [items[items.length - 1]];
}
}
template
<h2>list without</h2>
<div *ngFor="let item of items | lastItem:true">{{item}}</div>
<h2>list with</h2>
<div *ngFor="let item of items | lastItem:false">{{item}}</div>