confusedntired
confusedntired

Reputation: 137

Using reduceRight to remove last element in Array

I have a method to add a new list of dates that a User can request time off from work. Now I am trying to implement a way for the user to remove a date list starting from the bottom and working their way up as necessary but always keeping the first date there. Any suggestions would be appreciated

Here is the code I have for the 2 methods.

  dates = [1]

  onAddClicked() {
    this.dates.push(1);
  }

  onRemoveClicked() {
    this.dates.reduceRight(this.onAddClicked,);
  }

Here is the html for the buttons

<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>

The error Im getting is

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'

Fields for example

Upvotes: 0

Views: 121

Answers (1)

Dominik Matis
Dominik Matis

Reputation: 2146

Try to use

this.dates.pop();

if you also want to save that element, assign the result of pop() to some variable :)

Well, if you don't want to remove all, then do some magic like

if(this.dates.length > 1) this.dates.pop();

Upvotes: 1

Related Questions