Home > front end >  Can we convert formArray data into comma separated string, Example at index 0:["1,3,4,6"]
Can we convert formArray data into comma separated string, Example at index 0:["1,3,4,6"]

Time:07-30

Lets say i have a checkbox and each checkbox has a categoryId. When i check the checkbox i will get the categoryId and each data will be save as a formArray, example at index 0: current result

and this is the result i want to get expected result

CodePudding user response:

  convertValue(){
    const values = this.form.get('hubxCategoryId').value;

    //values is an array [true,true,false,...]
    return this.hubxReport
            //filter gets [{hubxCategoryId:2,categoryName:".."},
            //             {hubxCategoryId:6,categoryName:".."},..]
           .filter((x, index) => values[index]) 

            //maps return only the "Id" [2,6,...]
           .map(x=>x.hubxCategoryId)
           
            //join return an string "2,6,..."
           .join(",")
  }

But take a look to this SO, you not create a FormArray else a FormControl that store an array

CodePudding user response:

Try this: let desiredFormat= this.patientReportForm.value.hubxCategoryId.join(",")

  • Related