Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting a FormArray to an array angular

I have a FormArray of FormGroups, i want to iterate over the FormArray and detect which FormGroups are valid. However i am unable to do so as i am unable to iterate over a FormArray as it is not of type Array or string. My logic was to somehow cast the FormArray into an Array if possible. However i do not know the syntax in typescript for this.

 this.applicationFormArray = new FormArray([
            this.selectAppFormGroup = new FormGroup({
            }),
            this.generalAppFormGroup = new FormGroup({
            }),
            this.fileModeFormGroup = new FormGroup({
            }),
            this.accessListFormGroup = new FormGroup({
            }),
        ]);

checkValidity() {
        var foo = this.applicationFormArray as Array<>;
        for(var element of this.applicationFormArray){
            if(element.valid) {
                //do something
            }
        }
like image 990
ashley g Avatar asked May 06 '26 09:05

ashley g


1 Answers

You can convert a FormArray to a regular Array with the FormArray's value property. This is what worked for me. Using the value property on a FormGroup returns an object literal and on a FormArray it returns an array.

let newArr = this.applicationFormArray.value
newArr.map(...)

In your example it would look like this:

var foo = this.applicationFormArray.value;
for(var element of foo){
   if(element.valid) {
       //do something
    }
}
like image 140
Jay Jara Avatar answered May 08 '26 23:05

Jay Jara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!