0

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

1
  • 1
    splice should be used on an array (options), not an item in the array (options[indexOption]). Commented Apr 26, 2020 at 16:52

1 Answer 1

3

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
  );
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.