Tutorial - Using the some() method to check if a value is present in an array

In this tutorial, you will learn how to use the some() method inside a Cargo expression to check if a given string matches any of the elements in an array.

We will use an example that verifies if a person's job title matches any typical titles present in a list.


Pre-requisites

Before you begin

Make sure you have set up a sample data object that includes a job title for a contact. This object will serve as the starting point for the expression you'll create in this tutorial.

Also ensure, that you have a variable node containing an array of typical titles against which you want to check the job title.


Step 1

Identify the Website URL in the Start Node

Let's say we have the string containing the job title of a contact in the start node object.


Identifying the title field inside the start object

{{ nodes.start.title }}


Step 2

Assemble the list of titles in an array

Let's say we have a list of financial services industries that we are interested in, and we want to check if a person's job title matches any of these industries. Prepare an array of desired titles and a person's job title.


Capture the website URL with an expression

[
  'Professional services', 'Advertising Services', 'Fundraising', 
  'E-Learning Providers', 'Capital Markets', 'Credit Intermediation', 
  'Funds and Trusts'
]


Step 3

Use the some() method inside a Cargo expression

Use the some() method to check if the person's job title matches any of the desired titles. The some() method returns a Boolean value.

If the person's job title matches at least one of the desired titles, the expression will return True; otherwise, it returns False.


Use the some() method

{{ 
[
  'Professional services', 'Advertising Services', 'Fundraising', 
  'E-Learning Providers', 'Capital Markets', 'Credit Intermediation', 
  'Funds and Trusts'
].some(title => nodes.start.title.includes(title)) 
}}


Step 4

Use the some() method to check for the opposite

Conversely, if you wished to make the opposite operation, i.e. to check if the person's job title does NOT match any of the desired titles, you can use the some() method by using a ! symbol before defining the array.

If the person's job title does not match any of the desired titles, the expression will return True; otherwise, it returns False.


Use the some() method

{{ 
![
  'Professional services', 'Advertising Services', 'Fundraising', 
  'E-Learning Providers', 'Capital Markets', 'Credit Intermediation', 
  'Funds and Trusts'
].some(title => !nodes.start.title.includes(title)) 
}}

Outcome

Finish line

By this point, you have used the some() method to check if a person's job title matches any of the desired titles in a list of typical values.