Automation Blog

JavaScript Array Methods in Zapier Code Steps: A Complete Reference

Zapier Code steps often receive line-item data as comma-separated strings that need to be split, filtered, sorted, deduplicated, or transformed before passing to downstream steps. Here is every essential JavaScript array operation with copy-paste examples ready for Zapier's Code step environment.

ZapierJavaScriptCode Steps

By Troy Tessalone · · 8 minutes

Automation Guide

A practical field guide from Automation Ace.

JavaScript Array Methods in Zapier Code Steps: A Complete Reference

Line-item data from Zapier triggers — Looping by Zapier output, Formatter split results, webhook payloads — frequently arrives as arrays or comma-separated strings that need processing before they're useful. JavaScript's native array methods handle all of this: counting items, adding and removing elements, filtering to a subset, sorting, deduplicating, finding positions, getting min/max values, and joining back into strings for downstream steps. This guide covers every common array operation with examples ready to paste into a Zapier Code step.

Setting Up: Splitting a Comma-Separated String into an Array

Most Zapier line-item data arrives as a comma-separated string from inputData. The first step is splitting it into an actual array:

// Split comma-separated inputData into a trimmed array
const items = inputData.items.split(',').map(s => s.trim()).filter(s => s !== '');

The .filter(s => s !== '') removes any empty strings that result from trailing commas or double commas. Always include this when splitting user-provided or system-generated comma strings.

Count: Get the Number of Items in an Array

const items = inputData.items.split(',').map(s => s.trim()).filter(Boolean);

output = {
  count: String(items.length)
};

Add Items: push() and unshift()

push() adds to the end; unshift() adds to the beginning:

const items = inputData.items.split(',').map(s => s.trim()).filter(Boolean);

items.push(inputData.new_item);         // add to end
// items.unshift(inputData.new_item);   // add to beginning

output = {
  items: items.join(','),
  count: String(items.length)
};

Remove Items: pop() and shift()

pop() removes and returns the last item; shift() removes and returns the first:

const items = inputData.items.split(',').map(s => s.trim()).filter(Boolean);

const removed = items.pop();    // remove last item
// const removed = items.shift(); // remove first item

output = {
  items: items.join(','),
  removed_item: removed || '',
  count: String(items.length)
};

Filter: Include or Exclude Items Matching a Condition

const items = inputData.items.split(',').map(s => s.trim()).filter(Boolean);
const keyword = inputData.keyword.toLowerCase();

// Keep only items that INCLUDE the keyword
const included = items.filter(item => item.toLowerCase().includes(keyword));

// Keep only items that EXCLUDE the keyword
const excluded = items.filter(item => !item.toLowerCase().includes(keyword));

output = {
  included: included.join(','),
  included_count: String(included.length),
  excluded: excluded.join(','),
  excluded_count: String(excluded.length)
};

Remove Blank, Empty, or Null Values

const items = inputData.items.split(',');

// Remove items that are empty, whitespace-only, or the string 'null'
const cleaned = items
  .map(s => s.trim())
  .filter(s => s !== '' && s !== 'null' && s !== 'undefined');

output = {
  items: cleaned.join(','),
  count: String(cleaned.length)
};

Replace Blank Values with a Default

const items = inputData.items.split(',');
const defaultValue = inputData.default_value || 'N/A';

const filled = items.map(s => {
  const trimmed = s.trim();
  return (trimmed === '' || trimmed === 'null') ? defaultValue : trimmed;
});

output = {
  items: filled.join(',')
};

Remove Duplicates

const items = inputData.items.split(',').map(s => s.trim()).filter(Boolean);

// Case-sensitive deduplication
const unique = [...new Set(items)];

// Case-insensitive deduplication (preserves original casing of first occurrence)
const seen = new Set();
const uniqueCaseInsensitive = items.filter(item => {
  const key = item.toLowerCase();
  if (seen.has(key)) return false;
  seen.add(key);
  return true;
});

output = {
  items: unique.join(','),
  original_count: String(items.length),
  unique_count: String(unique.length)
};

Sort: Alphabetical and Numeric

const items = inputData.items.split(',').map(s => s.trim()).filter(Boolean);

// Alphabetical ascending
const sortedAlpha = [...items].sort();

// Alphabetical descending
const sortedAlphaDesc = [...items].sort().reverse();

// Numeric ascending (when items are numbers)
const numbers = inputData.numbers.split(',').map(s => parseFloat(s.trim())).filter(n => !isNaN(n));
const sortedNumeric = [...numbers].sort((a, b) => a - b);
const sortedNumericDesc = [...numbers].sort((a, b) => b - a);

output = {
  sorted_alpha: sortedAlpha.join(','),
  sorted_alpha_desc: sortedAlphaDesc.join(','),
  sorted_numeric: sortedNumeric.join(','),
  sorted_numeric_desc: sortedNumericDesc.join(',')
};

Always spread into a new array ([...items].sort()) before sorting — sort() mutates the original array in place.

Min and Max from a Numeric Array

const numbers = inputData.numbers.split(',')
  .map(s => parseFloat(s.trim()))
  .filter(n => !isNaN(n));

const min = Math.min(...numbers);
const max = Math.max(...numbers);
const sum = numbers.reduce((acc, n) => acc + n, 0);
const avg = numbers.length > 0 ? sum / numbers.length : 0;

output = {
  min: String(min),
  max: String(max),
  sum: String(sum),
  average: String(avg.toFixed(2)),
  count: String(numbers.length)
};

Find First or Last Index of an Item

const items = inputData.items.split(',').map(s => s.trim());
const searchValue = inputData.search_value.trim();

const firstIndex = items.indexOf(searchValue);      // -1 if not found
const lastIndex = items.lastIndexOf(searchValue);   // -1 if not found

output = {
  first_index: String(firstIndex),
  last_index: String(lastIndex),
  found: firstIndex !== -1 ? 'true' : 'false'
};

Find an Item at the Same Index Position Across Two Arrays

When two comma-separated arrays have paired values at the same position (e.g., product names and prices):

const names = inputData.names.split(',').map(s => s.trim());
const prices = inputData.prices.split(',').map(s => s.trim());
const searchName = inputData.search_name.trim();

const index = names.indexOf(searchName);
const matchedPrice = index !== -1 ? prices[index] : '';

output = {
  index: String(index),
  matched_price: matchedPrice,
  found: index !== -1 ? 'true' : 'false'
};

Join Array Items with a Delimiter

const items = inputData.items.split(',').map(s => s.trim()).filter(Boolean);

output = {
  comma_separated: items.join(', '),
  pipe_separated: items.join(' | '),
  newline_separated: items.join('\n'),
  bullet_list: items.map(item => `• ${item}`).join('\n')
};
These patterns cover the full lifecycle of array data in Zapier Code steps: receive as a comma string, split and clean, transform or filter, then join back to a string for the output object. The spread operator ([...arr]) before sort and reverse prevents in-place mutation bugs. Always convert output values to strings — Code step outputs must be strings or numbers, not arrays.

For string parsing operations like split, slice, and URL query params, see parsing and transforming strings in Zapier Code steps. For JSON parsing and number conversion, see parsing JSON and numbers in Zapier Code steps. For the full JavaScript environment reference, see JavaScript libraries in Zapier Code steps. For help building a data transformation workflow, talk to Automation Ace.

ZapierJavaScriptCode Steps

Disclaimer: This article may include links to apps, products, or services. Some links may be affiliate links, which means Automation Ace may earn a commission at no extra cost to you.

Build Better Systems

Ready to automate with confidence?

Share your tools, process, and goals. Automation Ace can design the workflow, integration, AI assist, or code bridge that fits your business.

Start a Project