JavaScript Array Handling Issue in Zapier Code Action

I’m having trouble with Zapier’s code action. When I use JSON arrays from another step, they get split into separate variables. But if I include the array directly in the code action, it works fine. Here’s what I’m trying to do:

let parsedData = JSON.parse(input.jsonData);
let originList = input.origins;
let originArray = originList.split(',');
let result = [];

parsedData.rows.forEach(row => {
  if (originArray.includes(row.product)) {
    result.push(row.id);
  }
});

return [{ id: 456, data: result.join(',') }];

Does anyone know why Zapier might be treating arrays differently when they come from other steps? How can I fix this so my code works with arrays from previous steps?

I’ve dealt with this issue in Zapier before. The problem stems from how Zapier handles data types between steps, often converting arrays to comma-separated strings.

To fix this, you can modify your code to handle both array and string inputs:

let originArray = Array.isArray(input.origins) ? input.origins : input.origins.split(‘,’);

This approach checks if input.origins is already an array and splits it if it’s a string. Additionally, ensure your previous step is outputting the array correctly. If you’re still having trouble, you might need to use a dedicated ‘Run JavaScript’ step for more control over data types. This step allows you to manipulate the data more freely before passing it to subsequent steps in your Zap.

hey there! i’ve seen this issue too, zapier flattens arrays from prev steps. try input.origins.split(‘,’) or check that your input is an array n’t a string. if that fails, consider a run javascript step. cheers!

I’ve encountered this exact problem before, and it can be frustrating. The issue lies in how Zapier handles data types between steps. When passing arrays between steps, Zapier often converts them to comma-separated strings.

To resolve this, you can try modifying your code slightly:

let originArray = Array.isArray(input.origins) ? input.origins : input.origins.split(',');

This checks if input.origins is already an array. If not, it splits the string. Also, ensure your previous step outputs the array correctly. Sometimes, explicitly stringifying and then parsing the array in your code can help:

let originArray = JSON.parse(JSON.stringify(input.origins));

If these don’t work, you might need to adjust how you’re passing data between steps or consider using a ‘Run JavaScript’ step for more control over data types.