user4347600
user4347600

Reputation:

How do i iterate through a formarray and set its value to another array in typescript?

I have a FormArray inside a form and the FormArray has a string parameter called "Foo". I tried to do:

let formArray = this.form.get("Foo") as FormArray;
let formArrayValues : {Foo : string}[];  //this will be put into the ts model to be sent out.

I am not sure how i can iterate through the FormArray, get the value "Foo" and put it into my new array. I tried :

for (let c of formArray.controls) {
   formArrayValues.push(c.get("Foo"));
}

but it seems i get error saying "Foo" is missing in type in AbstractControl. I am a bit lost here and I did search around but didn't find anything similar. Thanks

Upvotes: 0

Views: 1140

Answers (1)

Munzer
Munzer

Reputation: 2318

You don't need to iterate the form array, you can simply access the FormArray's value like this.

formArrayValues = [...formArray.value];

I suggest reading the original documentation to better understand what functionality is available for you and how to utilize form array to its max, just to avoid reinventing the wheel scenarios like this example (for example trying to iterate the form array control just to get the value when value is already available to you)

Upvotes: 1

Related Questions