cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
kaswiere
Microsoft
Microsoft

Change an object in an array

I have an array of objects called arrayVar that look like this

 

[{"LastName":"Smith", "FirstName":"Jeff"}, {"LastName":"Matt", "FirstName":"Mike"}]

 

How can I change the second element's FirstName?

 

I have tried use a compose where I do a setProperty(variables('arrayVar')[1], 'FirstName', 'Mark') and this doesn't seem to work.

9 REPLIES 9
PrasadAthalye
Community Champion
Community Champion

@kaswiere - You can do something like below...

  1.  declare empty array variable.
  2. Parse Json 
  3. Run loop on Parsed Json Array
  4. Add condition to check if its 2nd element.
  5. Add Compose to build string with new values
  6. And then use Append to Variable action and new compose... 
  7. At the end of this exercise, your array variable will have your expected values...

 

PrasadAthalye_0-1613865910116.png

 

Please Like and Mark this as Answer if it resolves your Issue.

Power Automate Community
Paulie78
Super User
Super User

Use this expression in a compose:

 

union
(
  take(variables('arrayVar'), 1), 
  array(setProperty(variables('arrayVar')[1], 'FirstName', 'Paul'))
)

Use a set variable action after the compose to set the value of the array var to the output of the compose

 

If you are trying to replace the first item in the list, that will work.  However, if you are trying to replace the second element in the list, I don't think this will work.

Paulie78
Super User
Super User

@kaswiere replace the second item is exactly what it does. Try it out.

You are right it does replace the second item.  However, what if you have four items and want to replace the second item?

Paulie78
Super User
Super User

Then you would do this instead:

union
(
  take(variables('arrayVar'), 1), 
  array(setProperty(variables('arrayVar')[1], 'FirstName', 'Paul')),
  skip(variables('arrayVar'), 2)
)
  • Take the first.
  • Modify the second.
  • Skip the first two and take the rest.

That will work.

 

Now, I have another problem.

 

If I have an array like this

[{"LastName":"Smith", "FirstName":"Tim"}, {"LastName":"Jones", "FirstName":"Matt"}]

 

and a second array like this

 

[{"LastName":"Smith", "FirstName":"Tim"}, {"LastName":"Jones", "FirstName":"Mark"}]

 

and do a union on both arrays, I am going to get an array like this back

 

[{"LastName":"Smith", "FirstName":"Tim"}, {"LastName":"Jones", "FirstName":"Matt"}, {"LastName":"Jones", "FirstName":"Mark"}]

 

 

 

Paulie78
Super User
Super User

Why is that a problem, that is exactly what you'd expect to get, the combination of the two arrays?

If you want to remove the duplicates, you just union it on itself. Like:

union(outputs('names'), outputs('names'))

and then you will just get the unique names.

 

My mistake.  I was not understanding your formula correctly.

Helpful resources

Announcements
 WHAT’S NEXT AT MICROSOFT IGNITE 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Register for a Free Workshop.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

September Events 2022

Check out all of these events

Attend in person or online, there are incredible conferences and events happening all throughout the month of September.

Users online (3,340)