0

I have declared the below form group.

     this.secondFormGroup = this._formBuilder.group({

  nested: this._formBuilder.group({
    arr1: [],
    arr2: [],
    arr3: [],
    arr4: []

  }),
 })

Once the user fills the form, i want to loop through the arrays in the form group. But i'm getting an error that foreach is not a function. what am i doing wrong?

This is what i tried.

var arrays =this.secondFormGroup.get('nested').value
arrays.forEach(x=>{
  if(x.arr1.length){
    x.arr1.forEach(x=>{
    console.log(arr1
    }) 
  }

2 Answers 2

1

nested is a group. You need to get the keys of the nested and iterate on that.

After first line, do something like this:

Object.keys(arrays).forEach(key => {
      arrays[key].forEach(x => {
         console.log(x);
      });
});
Sign up to request clarification or add additional context in comments.

Comments

0

You will get an object with arr1, arr2... as properties on it. Use Object.keys(arrays) to iterate through that object's properties.

Object.keys(arrays).forEach(key => {
  if(arrays[key].length) {
    arrays[key].forEach(value => console.log(value));
  }
});

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.