How to filter an array or line items using a Code Step

  • 26 August 2020
  • 6 replies
  • 4001 views
How to filter an array or line items using a Code Step
Userlevel 5
Badge

Hi there, this is Erin Oz from the Zapier Support Team with another workflow idea to share!

Often a user wants to perform actions in their Zap only on certain items in an array.

The Filter by Zapier step does not filter each item in an array, so the solution here is to use a Code Step. You can use the filter method in Javascript to only allow the items you’d like to pass into the next steps of your Zap. 

Example

This user has a Zap that triggers on a call ending. They would like to send a survey to all call participants that do not work at their company. What we’ll do here is use the filter method to only allow email addresses to continue in the Zap if they do not contain the @zapier.com domain. 

For example, let’s pretend we have a trigger step that returns an array of five email addresses - three of them end with @zapier.com, so we’d like to filter those out of the actions that come next. 

Our Code Step will accomplish three things: 

  1. All data that is brought into a Code Step is brought into the code as strings. In other words, even if the data being pulled in is already in an array, we will need to add code that turns the data into an array for the scope of the Code Step. 

const emailsArray = inputData.emails.split(",");

  1. The next line will filter out any email addresses containing ‘@zapier.com’. 

const filteredEmails = emailsArray.filter(email => !email.includes('@zapier.com'));

  1. The final line will output the array of filtered emails 

output = [{filtered_emails: filteredEmails}] 

 

The Input Data field is named emails, which is important to making this code work as expected! 

4YV1-STgZofwe9YMbPwnBZk7_ed_wvyHomVWWbj453kyLzaikzvItTxG5RcL2MjeO04ZgkUDHWeKvDBr-qjtVfS1V95DSK_7YewfgkEaLDtb5h4hsKiIpBosT_urKzoswP2IO0um

(view larger)

 

If you’d like to change this code to only allow emails that do contain ‘@zapier.com’, you can remove the “!” from !email.includes(‘@zapier.com)

You can also change the search string from ‘@zapier.com’ to anything you’d like to filter on. :sunglasses:

We can see from our results here that we’ve removed the three email addresses that contained @zapier.com from the array, and only the emails from @client.com are returned in the output! 

ijqdksAIuw2EtbMj6HkQILtZsJ_IypLvEHVxVMdLySAcIleFlP5Rj8WJbf4WqP-fJ-Akxaya2XwVzDNyEl-GKALUGMI99frAiKWoGsVZl3BdXojsD_rKZV0jpYPSzkOnzEbrY2xN

(view larger)

 

​Does this make sense? Please let me know if you have any questions or need additional clarity on any of these steps!

 


6 replies

Userlevel 5
Badge

As a followup, let’s say you were expecting a variable number of email addresses each time the Zap runs, and you wanted your action steps to be repeated for each item returned in the array. 

For example, you want to send an email both to external@client.com and external2@client.com, but next time your Zap runs there might be three clients! 

Check out Option 2 from @TimS’ excellent post here: 

 

Userlevel 7
Badge +10

Great post @erinoz - thanks!

Hi there @erinoz ! How can I do this with multiple conditions and multiple arrays?

Userlevel 1

How would I apply a filter to only return the elements in an array where one of the elements is a boolean value of ‘True’ (so don’t return the elements which are ‘False’ ?

Userlevel 7
Badge +12

@twelvetens Can you show us an example input? Thanks!

Userlevel 1

Sure thing! Here’s the output from my utilities step:


​​​​​​

What I want to have is a filtered array, so in the example above, I would get both lines, but if any lines had ‘selected’ as True AND had ‘selectable’ as False, then I would not this lines returned.

Important to note that I’d need all of the line items detail returned for lines which pass the above logic, if that makes sense? So that I can grab all elements (price, qty etc) to use in further zap steps….

Reply