0

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:[]
          }))
       // })
      })
      }
    }
    );
   
  }
6
  • Q: Have you successfully extracted the numbers you wanted from your data source, and copied them to an array of integers? Q: Aftwards, did you try "array.toString()"? Q: What did you get? Where are you "stuck"? Commented Jul 16, 2022 at 4:26
  • yes i have got a data like this example [0.{categoryId :1},1.{categoryId :2}] and so on but this is not the result i want. I want the result to be example "1,2,3,4" only the ids in one row. Please check the onbuttonclick function what i have to change there to get the result. i have tried toString on OnButtonClick function i got cannot use toString on type void. Commented Jul 16, 2022 at 4:33
  • hello i have made changes and i have got one question ill update the question please check Commented Jul 16, 2022 at 5:09
  • Are you having a result like this? i.ibb.co/qC7H2Q2/Screenshot-2022-07-16-092533.png Commented Jul 16, 2022 at 6:28
  • 1
    It sounds like you're almost there. See my example below. See also: javatpoint.com/javascript-create-and-download-csv-file Commented Jul 17, 2022 at 16:29

2 Answers 2

2

You can use join

const res = [1,2,3,4].join(',')
Sign up to request clarification or add additional context in comments.

1 Comment

how do i call res in my hubxcategory formarray
-1

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

1 Comment

thanks its working but i have one question how do i call csv in my hubxcategory formarray

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.