I encountered a problem while working on a function in Angular that involves pulling data from an API. My goal is to enhance a current segment to accommodate multiple IDs, but I face difficulties when attempting to retrieve more than one ID for the API query.
if (this.selectedCustomer !== null) {
filterString += `&customer_id=[${this.selectedCustomer.id}]`;
}
The above code snippet results in &customer_id=[1]
, whereas I aim to have something like &customer_id=[1,2,3]
.
In an attempt to achieve this, I drafted replacement code where I tried to compile the IDs into an array and pass them along the query. However, the loop isn't functioning as expected, leading to 'undefined' IDs.
if (this.selectedCustomer !== null) {
const customerIds = this.selectedCustomer.id.customerIds;
for (let i = 0; i < customerIds.length; i++) {
customerIds[i].selected = this.selectedCustomers.indexOf(customerIds[i].value) > -1;
}
this.selectedCustomer.id = customerIds;
filterString += `&customer_id=[${customerIds + ','}]`;
}
If anyone can offer guidance or point out my mistake, it would be greatly appreciated! :)