Home > Software design >  Delete certain property records in a form array using forEach
Delete certain property records in a form array using forEach

Time:03-02

I wish to loop through a form array and remove a certain property (in this case I want to remove the newLanguage property).

Would the approach be:

this.myForm['controls'].languages['controls'].forEach((element) => {
 delete element['controls'].newLanguage;
});

CodePudding user response:

If you want to exclude newLanguage value form formGroup, Then you can disable formControl by calling disable method.

this.myForm['controls'].languages['controls'].forEach((element) => {
    element['controls'].get('newLanguage').disable();
});

OR

If you want to remove formControl from FormGroup then you need to call removeControl method and pass the control name to it

this.myForm['controls'].languages['controls'].forEach((element) => {
    element['controls'].removeControl('newLanguage');
});
  • Related