Filtering to include/exclude items in an array, using Code by Zapier

  • 24 November 2021
  • 5 replies
  • 320 views
Filtering to include/exclude items in an array, using Code by Zapier
Userlevel 7
Badge +13

📌  We do our best to ensure that the code in these articles works as advertised, but please be aware that Zapier Support does not officially help with code steps due to their advanced nature. Feel free to comment on the article or ask in the Zapier Community, where we have code-savvy folks (including the author of this post).

Q: Ever wanted to filter to include/exclude items in an array?
A: Use a Code app as a Zap step: https://zapier.com/apps/code/help

 

Code step configuration using JavaScript

NOTE: Map your line items array to the right side value for the “Set” Input Data variable in place of “A,B,C,C,B,A”.

 

Filter By to INCLUDE values in array

 

COPY THE CODE

let Set = inputData.Set.split(","); // creates array by splitting Input Data Variable at commas

let FilterBy = inputData.FilterBy; // value to filter the array by

Set = Set.filter(i => i.includes(FilterBy)); // INCLUDE values in array

output = [{Set}];

 

Filter By to EXCLUDE values in array

 

COPY THE CODE

let Set = inputData.Set.split(","); // creates array by splitting Input Data Variable at commas

let FilterBy = inputData.FilterBy; // value to filter the array by

Set = Set.filter(i => !i.includes(FilterBy)); // use ! EXCLUDE values in array

output = [{Set}];

 

RESULTS

Code step results to INCLUDE values in an array.

 

Code step results to EXCLUDE values in an array.

 

Contribution by Troy Tessalone

Troy is a Certified Zapier Expert who automates workflows with no-code and low-code apps to help clients save time and make money.


5 replies

This is super helpful but I have an array of number (0.00, 5.00, 10.00, and 20.00) and I am trying to exclude the 0.00.  When I set the FilterBy to 0.00, it excludes 0.00 but also excludes 10.00 and 20.00.  Any ideas?

Userlevel 7
Badge +13

Hi @erictobias 

Try changing the filter line to this:

 

Amazing!  Thank you so much!  It now returns 20.00, 10.00, and 5.00 as intended.  If I add Set.sort(); it returns 10.00 before 5.00.  Any idea how to solve that?

Userlevel 7
Badge +13

@erictobias 

Try adding this line after the filter line:

 

amazing. thank you!

Reply