RRB
RRB

Reputation: 2116

Remove certain item from array

I am struggling to remove an item from an array. The item needs to be removed dynamically.

Here is my code

Array

{
  "corporateId": "be67e184-a663-439c-b841-c14a734011eb",
  "selectedMAP": [
    {
      "mapId": 79,
      "mapName": "discovery",
      "active": true,
      "options": [
        {
          "optionId": 116,
          "optionName": "comprehensive",
          "memberAmount": 2000,
          "adultDependantAmount": 1500,
          "childDependantAmount": 500,
          "active": true
        }
      ]
    },
    {
      "mapId": 80,
      "mapName": "goodhealth",
      "active": true,
      "options": [
        {
          "optionId": 117,
          "optionName": "keycore",
          "memberAmount": 1000,
          "adultDependantAmount": 600,
          "childDependantAmount": 400,
          "active": true
        },
        {
          "optionId": 119,
          "optionName": "keycore2",
          "memberAmount": 500,
          "adultDependantAmount": 300,
          "childDependantAmount": 200,
          "active": true
        }
      ]
    }
  ]
}

TS

removeOption(index: number, indexOption: number) {
  this.companyMedicalAidProvider[0].selectedMAP[index].options[indexOption].splice(indexOption, 1);
}

HTML

<div id="medicalCard" class="medicalCard" *ngFor="let provider of companyMedicalAidProvider; let i = index;">
...
 <div id="option" class="row option" *ngFor="let providerOptions of companyMedicalAidProvider[0].selectedMAP[i].options; let b = index;">
  <button (click)="removeOption(b,i)">X</button>
 </div>
...
</div>

The error I see in my console is ERROR TypeError: this.companyMedicalAidProvider[0].selectedMAP[index].options[indexOption].splice is not a function

Upvotes: 0

Views: 55

Answers (1)

Zachary Haber
Zachary Haber

Reputation: 11037

You have to call splice on the array not on the object itself. Changing it to options.splice should work:

removeOption(index: number, indexOption: number) {
  this.companyMedicalAidProvider[0].selectedMAP[index].options.splice(
    indexOption,
    1
  );
}

Upvotes: 3

Related Questions