KingCk
KingCk

Reputation: 153

How to convert formArray data into comma separated string

I have got the data as [0:1,1:2,2:3] here 0,1,2 at the left side is array index but i dont want this data format i want it as ['1','2','3'] as a string.
Here's my code

onChange(categoryId: string, isChecked: boolean) {
  debugger
  const categoryIdArray = (this.patientReportForm.controls.hubxCategoryId) as FormArray;
  
  if (isChecked) {
    categoryIdArray.push(new FormControl(categoryId));
  } else {
    let index = categoryIdArray.controls.findIndex(x => x.value == categoryId)
    categoryIdArray.removeAt(index);
  }
}

in categoryIdArray i tried to use join function as

categoryIdArray.join(",")

here i got an error - Property 'join' does not exist on type 'FormArray'

this is my formcontrol

this.patientReportForm = this.formBuilder.group({
      patientId : new FormControl(Number(this.clientId)),
      hubxCategoryId : this.formBuilder.array([]),
      notes : new FormControl(),
    })

Upvotes: 0

Views: 236

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522817

Assuming your input also be an array of strings, we can try using map() here along with split():

var input = ["0:1", "1:2", "2:3"];
var output = input.map(x => x.split(":")[1]);
console.log(output);

Upvotes: 3

Related Questions