Angular 2 - Create array of objects from FormArray

Chachi_Arcola picture Chachi_Arcola · Aug 10, 2017 · Viewed 11.2k times · Source

I have a form that contains a FormArray of people. The people array then contains a FormArray of addresses which gives me the following JSON:

{
  "people": [
    {
      "lastName": "Last-1",
      "firstName": "First-1",
      "middleName": "",
      "addresses": [
        {
          "street": "Person1 - Address1"
        },
        {
          "street": "Person1 - Address2"
        }
      ]
    },
    {
      "lastName": "Last-2",
      "firstName": "Last-2",
      "middleName": "",
      "addresses": [
        {
          "street": "Person2 - Address1"
        }
      ]
    }
  ]
}

Now I'm trying to take these "people" from the form and create an array of Person objects

export class ReportPerson {    
    lastName: string = '';
    firstName: string = '';
    middleName: string = ''; 
    addresses: PersonAddress[];
}
export class PersonAddress {
        street: string = '';   
}

When I use console.log(form.get('people').value); I get the following result:

(2) [{…}, {…}]0: {lastName: "Last-1", firstName: "First-1", middleName: "", 
addresses: Array(2)}
              1: {lastName: "Last-2", firstName: "Last-2", middleName: "", 
addresses: Array(1)}length: 2__proto__: Array(0)

But no matter how I try to get the data, it says that my list is undefined. For example, the following returns that it can't read "lastName" of undefined.

 save(form: any) {
        var reportPersonList: ReportPerson[] = new Array();
        var people = form.get('people');

        for (let i = 0; i < people.length; i++) {
            console.log(people[i].lastName);
       }
    }
} 

My question is, what is the proper syntax to create an array of people objects from the data in the FormArray? I know it's something basic that I'm missing, but I'm used to C# and new to Typescript/Angular2.

Answer

Chachi_Arcola picture Chachi_Arcola · Aug 14, 2017

For anyone else that runs across this problem, I was able to solve it by creating a new form array from the values of the 'people' form array. Then using form array functions to map the form values to the object values. Then I repeated the process for the array of addresses.

save(form: any) {
    var reportPersonList: ReportPerson[] = new Array();

    var people = form.get('people') as FormArray;

    for (let i = 0; i < people.length; i++) {
        var p = new ReportPerson;
        p.lastName = people.at(i).get('lastName').value;
        p.firstName = people.at(i).get('firstName').value;
        p.middleName = people.at(i).get('middleName').value;
        var addresses = people.at(i).get('addresses') as FormArray;
        for (let j = 0; j < addresses.length; j++) {
            var a = new PersonAddress;
            a.street = addresses.at(j).get('street').value;

            p.addresses.push(a);
        };
        reportPersonList.push(p);
    }
    this.reportFormDataService.setReportPeople(reportPersonList);

}