Power Automate Array indexOf with Real-Time Examples

Power Automate Array indexOf: Real-Time Examples

No comments

Loading

In this “Power Automate Array IndexOf” article, we will learn how to extract a particular element from the Power Automate array variables with real-time project examples or how to get array elements by their value in Power Automate.

Power Automate, Microsoft’s robust automation tool, empowers users to streamline workflows by automating repetitive tasks. One common requirement is finding the index of an item within an array. The indexOf function proves indispensable in such scenarios, allowing users to easily locate elements within an array. In this article, we’ll explore a real-time project example to demonstrate how to effectively use the indexOf function with arrays in Power Automate.

What is an Array in Power Automate?

In Power Automate, an array is a fundamental data structure that allows you to store and manage multiple related values under a single variable. It’s a collection of elements, where each element can be of any data type (e.g., numbers, strings, objects) and can be accessed using an index. Arrays are incredibly versatile and useful when working with a set of related data.

Key features of arrays in Power Automate include:

  • Collection of Elements: An array can hold a collection of elements, and each element is identified by its position or index within the array.
  • Sequential Order: Elements in an array are ordered sequentially, typically starting from index 0 for the first element, then 1 for the second, and so on.
  • Heterogeneous Data Types: Arrays can hold elements of different data types (e.g., numbers, strings, objects) within the same array.
  • Fixed or Dynamic Size: Arrays can be of fixed size (static) or dynamic size (dynamic). Dynamic arrays can grow or shrink during runtime.
  • Accessing Elements: Elements within an array can be accessed using their index, allowing for easy retrieval and manipulation of data.

In Power Automate, arrays are commonly used when dealing with data from various sources, such as SharePoint lists, Excel spreadsheets, HTTP responses, or JSON outputs. They enable the automation of complex workflows by handling and processing multiple pieces of data efficiently.

Here’s a simple example of an array in Power Automate, represented in JSON format:

["Apple", "Banana", "Cherry", "Date"]

In this array, we have four elements, each representing a type of fruit.

Understanding and effectively using arrays is crucial for building robust Power Automate flows, allowing you to manipulate and process data efficiently.

Understanding the indexOf Function

The indexOf function in Power Automate is used to determine the position of a specified item within an array. If the item is not found, it returns -1. If the item appears multiple times, it returns the index of the first occurrence.

The syntax is as follows:

indexOf(<array>, <item>)
  • <array>: The array in which to search for the item.
  • <item>: The item to find the index of within the array.

Power Automate Array indexOf: Real-Time Examples

In these demos, we will learn how to extract specific values from the Power Automate Array using the indexOf() function, the lastIndexOf() function, and the nthIndexOf() function.

Scenario 1: Access Array Elements: Get a specific employee name from the employee array

Let’s consider a practical scenario where we have a list of employees, and we need to find the index of a specific employee by their name and extract the last name from a given employee configured in the array.

Scenario

Suppose we have an array of employee names:

[
"John Doe",
"Jane Smith",
"Michael Johnson",
"Sarah Davis",
"Kevin Brown"
]

Initialize Variable Type Array Power Automate

Step 1: Add Initialize variable action from the variable section

Add Initialize variable action in Power Automate
Add Initialize variable action in Power Automate

Step 2: Create and initialize a variable type array with a default value

Create and initialize a variable type array with a default value
Create and initialize a variable type array with a default value

Now, let’s access a specific element from this array. For example, if we want to extract “Michael Johnson” from this array, we need to write the below expression:

Expression: @{variables('varArrayEmployee')[2]}

Then, if we want to extract the last name from this particular array element, we need to write the below expression, where we need to use the Power Automate Index function and substring function.

substring(variables('varStrFromArray'),indexOf(variables('varStrFromArray'),' '))
Access specific array elements in Power Automate Array
Access specific array elements in Power Automate Array

Output:

Access specific array elements in Power Automate Array demo output
Access specific array elements in Power Automate Array demo output

Loop through Array Power Automate: Loop through each item in Power Automate Array

Add an “Apply to each” control and configure it as below to loop through each item in the Power Automate array.

Loop through each item in Power Automate Array
Loop through each item in Power Automate Array

 

Scenario 2: Extract a product from the Product Array

Imagine we have a list of products in an e-commerce system, and we want to determine whether a specific product is available in stock based on its name. We will use the indexOf function to find the index of the product name in the array. If the product is found, we’ll consider it in stock; otherwise, it’s out of stock.

Step 1: Create a product array and a string variable like below:

Create product array in Power Automate
Create product array in Power Automate

Step 2: Loop through each item in Power Automate Product Array

Then, loop through each item in the given product array.

Store each current item in a string variable using the item() function.

Then, check whether the current item is ‘Laptop’ or not; if yes, the product is in stock; otherwise, it is out of stock.

Loop through each item in Power Automate Product Array
Loop through each item in Power Automate Product Array

We also can use the below expression inside the loop:

if(equals(indexOf(variables('varStrProductArray'), 'Laptop'), -1), 'Out of Stock', 'In Stock')

 

Summary: Power Automate Array indexOf: Real-Time Examples

Thus, in this article, we have learned how to extract the specific value from the Power Automate Array using the indexOf() and substring() functions in Power Automate.

These examples demonstrate the practical application of the indexOf function in determining the availability of a product in an inventory system. By utilizing this function, users can quickly find the position of an item within an array and make informed decisions based on its presence or absence. Incorporating the indexOf function into Power Automate flows enhances the efficiency and accuracy of automation processes.

See Also: Power Platform Articles

You may also visit the Power Platform article hub, where you will see a bunch of articles focusing on Power Platform, like Power Automate, Power Apps, etc. All the articles are written with real-time project scenarios and troubleshooting techniques.

About Post Author

Do you have a better solution or question on this topic? Please leave a comment