Using JavaScript in Zapier to Divide a String by '-' into Separate Outputs

I am attempting to parse a string formatted like this:

'- Item A - Item B - Item C'

I want to use JavaScript to split these items into distinct outputs for Zapier. The aim is to populate a form for a client with each split item, such as:

mywebsite.com/form/?field1=Item%20A&field2=Item%20B&field3=Item%20C

It’s essential that the solution accommodates a variable number of data lines, ranging from one to potentially twenty. Additionally, I’d like to know if there are alternative approaches besides using JavaScript. Anyone have suggestions?

I’ve found using JavaScript in a Code by Zapier step to be quite effective for what you’re trying to achieve. You can use the split method to divide the string by the hyphen and then map the results to build your URL. Something like this would work:

let items = inputData.string.split(' - ').filter(Boolean);
let result = '';
items.forEach((item, index) => {
    result += `&field${index + 1}=${encodeURIComponent(item.trim())}`;
});
output = {url: 'mywebsite.com/form/?' + result.slice(1)};

This will handle varying numbers of items by iterating through all parts and appropriately constructing the URL.