KingCk
KingCk

Reputation: 153

how can i convert my array data into comma separated string

In below onButtonClick there is Ids variable i want to call only that data in my hubxCategoryId formcontrol. and i want to show the data as a comma separeted string

   onButtonClick(value:any,categoryIds:number, index:number):any{
        debugger
        const formArray = this.patientReportForm.get("hubxCategoryId") as FormArray;
        var x = formArray.controls.find(x => x.get('catId').value === categoryIds )
        var currentindex = formArray.controls.indexOf(x);   
        ((this.patientReportForm.get('hubxCategoryId') as FormArray).at(currentindex) as FormGroup).get('categoryId').patchValue(value) ; 

let Ids : Array<any> = []; 
    formArray.value.forEach( (item) => {
      Ids.push(item.categoryId)
    })

    return Ids
    
    }

This is my formcontrol .I want to call Ids in HubxCategoryId

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

array is printing from here

getHubxReport() {
    //debugger
    this.usersService.getHubxReport(this.clientId).subscribe((response: ResponseModel) => {
      if (response != null && response.data != null && response.data.length > 0) {
        this.hubxReportList = response.data;
        const formArray = this.patientReportForm.get("hubxCategoryId") as FormArray;
        response.data.forEach(item => {
        formArray.push(this.formBuilder.group({
          catId: item.categoryId,
          categoryId:[]
          }))
       // })
      })
      }
    }
    );
   
  }

Upvotes: 0

Views: 1958

Answers (2)

paulsm4
paulsm4

Reputation: 121629

Assuming you're generating formArray correctly, your onButtonClick() event handler could do something like this:

let formArray =  [
  {"categoryId" :1},
  {"categoryId" :2}
];
console.log("original js:", formArray)
let ids = [];
formArray.forEach( (element) => {
  ids.push(element.categoryId);
});
console.log("new array:", ids);
let csv = ids.join(",");
console.log("csv:", csv);

Upvotes: -1

Drenai
Drenai

Reputation: 12347

You can use join

const res = [1,2,3,4].join(',')

Upvotes: 2

Related Questions