Home > Enterprise >  how can i convert my array data into comma separated string
how can i convert my array data into comma separated string

Time:07-18

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(),
    })

CodePudding user response:

You can use join

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

CodePudding user response:

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);

  • Related