JavaScript Help: Converting Paired Lists to CSV Format in Zapier

Hi everyone! I need some JavaScript assistance for a Zapier code step. I’m working with two comma-separated inputs that need to be combined.

ProductIDs: 101,102,103
CategoryIDs: 5,6,7

Both inputs always have the same number of items, and the position matters. I need to create a CSV output that pairs each item based on its position.

Expected result:

101,5
102,6
103,7

Can someone share JavaScript code to accomplish this? I’m not experienced with coding and would really appreciate any help!

I use Zapier for this stuff all the time. Here’s a cleaner approach with a regular for loop, especially for bigger datasets:

const products = inputData.ProductIDs.split(',');
const categories = inputData.CategoryIDs.split(',');
const csvLines = [];

for (let i = 0; i < products.length; i++) {
    csvLines.push(products[i].trim() + ',' + categories[i].trim());
}

output = [{csv_output: csvLines.join('\n')}];

This works great when you need to add validation or extra logic later. You can easily throw in a check to make sure both arrays match in length before processing. Performance is basically the same for normal Zapier workflows, but this structure makes debugging way easier when stuff breaks.

Here’s a one-liner if you want shorter code:

output = [{csv_output: inputData.ProductIDs.split(',').map((prod, i) => prod.trim() + ',' + inputData.CategoryIDs.split(',')[i].trim()).join('\n')}];

Does the same thing, just more compact. Works great in Zapier!

Here’s a simple JavaScript solution for your Zapier code step:

const productIDs = inputData.ProductIDs.split(',');
const categoryIDs = inputData.CategoryIDs.split(',');

const csvRows = productIDs.map((productID, index) => {
    return productID.trim() + ',' + categoryIDs[index].trim();
});

const result = csvRows.join('\n');

output = [{csv_output: result}];

This splits both input strings by commas, then pairs each product ID with its matching category ID using the same index position. The trim() removes any extra spaces around values. Finally, it joins everything with newlines to create your CSV format. Just make sure your Zapier input field names match what you’re referencing in the code.