Search code examples
angularangular-materialangular-material-stepper

Angular delete object at index in stepperArray


Before I make this API call I need to remove the product that contains a selectedPlan value of null. I am unsure as to why the below is not working. Do I need reassignment of stepper array before passing as parameter in startEnrollment()?

startEnrollment(stepperArray: MhnStartEnrollmentRequest[]) {
        stepperArray.forEach(value => {
          if (value.selectedPlan === null) {
            delete stepperArray[value.productId]
          }
        });
        stepperArray.values();
        return this.mhnApiClientService.startEnrollment(stepperArray, this.quoteId, this.clientId);
      }

enter image description here


Solution

  • Your accessing product to delete by value.productId instead of its actual index in the array

    And instead of deleting it which just makes the value at that index undefined use filter

    const enrolmentsWithAPlan = stepperArray.filter(value => value.selectedPlan !== null);