![](/img/trans.png)
[英]How to calculate sum of last n elements of an array using Arr.reduceRight
[英]Using reduceRight to remove last element in Array
我有一種方法可以添加新的日期列表,以便用戶可以要求下班時間。 現在,我正在嘗試為用戶提供一種從底部開始刪除日期列表,並根據需要向上移動日期列表的方法,但始終將第一個日期保留在那里。 任何建議,將不勝感激
這是兩種方法的代碼。
dates = [1]
onAddClicked() {
this.dates.push(1);
}
onRemoveClicked() {
this.dates.reduceRight(this.onAddClicked,);
}
這是按鈕的html
<div class=modButtons>
<button (click)="onAddClicked()" mat-raised-button color="primary">Add Dates+</button>
<button (click)="onRemoveClicked()" mat-raised-button color="primary">Remove Dates-</button>
</div>
我得到的錯誤是
Argument of type '() => void' is not assignable to parameter of type '(previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number'.
Type 'void' is not assignable to type 'number'
嘗試使用
this.dates.pop();
如果您還想保存該元素,則將pop()
的結果分配給某個變量:)
好吧,如果您不想刪除所有內容,那么可以做一些魔術,例如
if(this.dates.length > 1) this.dates.pop();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.