Skip to main content

Notifications

Power Automate - AI Builder
Suggested answer

How to Merge 2 Arrays without using Apply To Each Loop

(1) ShareShare
ReportReport
Posted on by 3
Hi everyone, I am currently trying to merge two arrays without having to use an apply to each loop as it is very inefficient.

I have my first array that looks something like this: [1,2,3,4,5,6,7,8,9,10]
And I have my second array that is an array of items: [{'ID': 3, 'data':'data3},{'ID': 5,'data':'data5'},{'ID': 6,'data':'data6'},{'ID': 8,'data':'data8'}}
And the resulting array needs to be something like: [1,2,{'ID': 3, 'data':'data3},4,{'ID': 5,'data':'data5'},{'ID': 6,'data':'data6'},7,{'ID': 8,'data':'data8'},9,10]

I managed to use a for loop and match each number to it's corresponding ID by checking if there is a valid id and if so update the array to include the item and if not just leave the number as is.

However I wonder if I can do this without using a for loop as it takes super long and is inefficient with larger data sets, I am open to ideas!

Thank you.
Categories:
  • Suggested answer
    Nived_Nambiar Profile Picture
    Nived_Nambiar 17,105 on at
    How to Merge 2 Arrays without using Apply To Each Loop
    Hi,
     
    Yes it is possible by using xpath function. See below
     
     
     
    Use compose action to make the array2 prior to converting to xml as shown below
     
    Now in select action
     
    Now the main logic 
     
    Expression used- 
    if(equals(length(xpath(xml(outputs('Compose')),concat('//data[ID="',item(),'"]'))),0),item(),json(first(xpath(xml(outputs('Compose')),concat('//data[ID="',item(),'"]'))))?['data'])
     
     
    See the result of select action
     
    Hope it helps !
    Thanks & Regards,
    Nived N 
    LinkedIn: Nived N's LinkedIn
    YouTube: Nived N's YouTube Channel
    Blog: Nived Nambiar's Blogs
     Found my answer helpful? Please consider marking it as the solution!
     Your appreciation keeps me motivated. Thank you!

     
  • FLMike Profile Picture
    FLMike 31,580 on at
    How to Merge 2 Arrays without using Apply To Each Loop
    Hi
     
    Based on what you need, not sure what more you could do.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,636

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,942

Leaderboard