Bring comma-separated values in a field into an array

This must be straightforward but I just can’t get my head around it!

I have a field containing comma-separated values (eg. 2,5,11,17) which I want to turn into an array for easy manipulation before then turning back to a comma-separated value to write back to the database.

I’m looking into split, repeats, arrays, array->add value, etc. but getting nowhere!

Can anyone point me in the right direction, please?

Split is the way to go to converting a comma separated list to an array.

field.split(',')

Where are you doing this, on the client-side or in server connect and what do you want to do with the values?

Thanks for your help @patrick.

It’s an e-commerce site so I have products in the database and will be showing related products on the detail page. Initially I worked with a many-to-many table but then thought that was overkill so having a single varchar field containing ids of the related products separated by commas would be more efficient.

So I have a mix of server connect and client-side. The front-end site is working fine - I’m splitting the field in server connect to give the query output and that’s all fine.

However, the admin area where the shop owner can maintain everything is where I’ve hit a bit of a wall.

So I want to turn the query output from server connect into an array, manipulate that client-side and then send back to server connect to store again as the comma-separated value.

Does that makes sense?

Perhaps following tutorials can help you

The tutorial is to display comma separated values with a checkbox group. You can then edit the values and submit it back to the server. The submitted values are an array, so in the insert query you do a join on the array to insert it as a comma separated value again.

Instead of checkboxes you could also use a multiselect or other component that supports multiple values.

Thanks again. Can I easily convert the array to a comma-separated value in the client-side? The communication with server connect could be as simple as receiving and sending back that one field?

On the client-side you can also convert the array to a comma-separated value, put it in a hidden field for example with a dynamic value. I think however that doing it the server action in the insert step is easier.

Sorry @patrick, how can I initially populate the array with the results from the server connect query output? I want the array to be populated when a modal opens up.

Don’t worry, I’ve sorted it. Can’t remember exactly what I was doing wrong but it’s fine now. :slight_smile: