Do you need to check if a certain value exists in an array?
Arrays are used to store multiple items in a single variable. Arrays are often what is returned when connecting to external data sources in your flow.
A common task when handling arrays is to check if the array contains a specific value.
This article will explore a few methods on how to perform this check in Power Automate.
What is an Array?
An array is a data structure in Power Automate that stores multiple values in a single variable. For example, you might use an array to store names, email addresses, or ages.
["Mark", "Jennifer", "Steve"]
The above example shows an array of 3 names.
Check if Array Contains a Specific Value with the Contains Function
In Power Automate, you can use the contains function to check if an array contains a specific value.
The contains function has a simple syntax, ideal for implementing it in your workflow.
<array>: The array you want to search in.
<value>: The value you’re looking for within the array.
The above example of the contains function will check the array in the Compose action and see if it contains the name Jennifer.
It returns true if the item is found and false otherwise.
📝 Note: The contains function works with exact matches only. For example, searching for Jen will result in a false value from the contains function even though the array contains a Jennifer.
Check if Array Contains a Specific Value with the Filter Action
In Power Automate, you can use the Filter array action to check if an array contains a specific value.
- First, add a Filter array action in your flow.
- Provide the array you want to check in the From input fields.
- Add an
item()expression in the Choose a value field on the left side of the condition. This allows the filter action to check each item in the array.
- Select the is equal to condition from the dropdown list.
- Add the item you want to search the array for in the Choose a value field on the right side of the condition. In this example, the Filter array action will return all the items in the array that equal the text Jennifer.
If the value exists in the array, the Filter array will return an array with at least one value. This might be sufficient, but if you need you can use another expression to test if the output contains an items.
The above expression will return true if the item exists in the array and false otherwise.
The length function returns the number of items in the output array. If this is not equal to zero, then the specific item must exist in the original array.
Check if Array Contains a Partial Match Value with the Filter Action
The Filter array action can also be used to test for a partial match in your array with a slight modification.
Select the contains option for the condition and now the Filter array action will return any item in the array that contains the text you’re testing.
Check if Array Contains a Specific Value with the Condition Action
The Condition action has a similar condition interface as the Filter array action and will allow you to create a similar test for a specific value in an array.
Here is how to use the Condition action to check if an array contains a specific value.
- Add the Condition action to your flow.
- Add the array you want to test to the Choose a value field on the left.
- Select the contains option as the condition from the dropdown.
- Add the value you want to check to the Choose a value field on the right.
If the array contains the specific value, the condition will be evaluated as true, and the actions inside the Yes branch will execute. If the array does not contain the value, the actions inside the No branch will execute instead.
Checking if an array contains a specific value can be done in several ways.
Using the contains function, the Filter array action, or the Condition action are all potential solutions.
The Filter array action has the added benefit that it allows you to check for exact or partial matches within the array.
What is your preferred method for checking values in your arrays? Let me know in the comments.