Replace array element using Power automate

Loading

Requirement: Replace array elements using Power Automate.

For example, if array [12,14,15,16,19,20] and replace 16 with 17 so new array will be [12,14,15,17,19,20].

Click on +create from Home page, select instant cloud flow provide name to the flow, select manually trigger a flow and click on create button.

Then Select initialize variable action, and then provide the variable name, type as an array, and set the value with the sample array.

Click on the Next step, select Initialize variable action with type as Integer and array, as shown in below:

MS Flow replace array

Now, click on the Next step, and select Apply to each action, and in the output set the ArrayID.

Inside Apply to each, click on Add an action and select Increment variable, and set the variable name as Index, and in value as 1.

After that click on the Add an action, and select the Append to array variable action. Then choose the Name as Temp New, and in value write like the below screenshot.

MS Flow replace array

Now set the array variable as empty, so click on the next step, select Set variable action, and choose the variable name ArrayID and value is [].

MS Flow replace array

Now add apply to each array variable to replace the value, so click on the Next step and select Apply to each action. In input select the Temp New variable, from the dynamic content.

Inside Apply to each action, click on the Add an action, and select Condition action, in value write the below expression is equal to 4.

items('Apply_to_each_2')['Index']

If the condition is true, in the If Yes part of the condition, add an action and select Append to array variable2, then select the variable name as ArrayID and in value write the below expression.

add(items('Apply_to_each_2')['Value'],1)

And If the condition is false, then in If No part, append the array variable, select the name as ArrayID, and in value write the below expression.

items('Apply_to_each_2')['Value']
MS Flow replace array

Next, to see the result, click on the Next step and select Compose action, and in Input select ArrayID variable.

MS Flow replace array

Now click on Save and run the flow manually, the result in the compose output. So here element index 4 value 14 gets replaced with 15.

MS Flow replace array

Leave a Reply

Your email address will not be published. Required fields are marked *