cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
dgonzalesjr
Regular Visitor

Compare two array's by object and compose unique records only

Hello everyone,

 

I have two arrays of objects that I'm trying to compare. I only want to keep the records that are unique and write them to an excel table. I know how to write to an excel table so for time sake we can just add the unique values to a separate variable.

 

Below are images of the two separate arrays  I want to compare both by "ID" and only keep the two records that are new. In this case it would be records with IDs 5 and 6.

Array1                                                             Array2

Screen Shot 2021-08-31 at 10.37.00 AM.png Screen Shot 2021-08-31 at 10.37.20 AM.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Here is the code snippet with the two arrays as well:

 

Array1

 

 

 

[
  {
    "ID": 1,
    "Status": "",
    "Region": "NAMER",
    "Program Name": "Fun Run",
    "Tracked in MAP?": "Yes",
    "Program Manager": "",
    "Collaborator": ""
  },
  {
    "ID": 2,
    "Status": "",
    "Region": "NAMER",
    "Program Name": "Fun Run",
    "Tracked in MAP?": "Yes",
    "Program Manager": "",
    "Collaborator": ""
  },
  {
    "ID": 3,
    "Status": "",
    "Region": "NAMER",
    "Program Name": "Long Run",
    "Tracked in MAP?": "Yes",
    "Program Manager": "",
    "Collaborator": ""
  },
  {
    "ID": 4,
    "Status": "",
    "Region": "NAMER",
    "Program Name": "Long Run",
    "Tracked in MAP?": "Yes",
    "Program Manager": "",
    "Collaborator": ""
  },
  {
    "ID": 5,
    "Status": "",
    "Region": "NAMER",
    "Program Name": "Hill Run",
    "Tracked in MAP?": "Yes",
    "Program Manager": "",
    "Collaborator": ""
  },
  {
    "ID": 6,
    "Status": "",
    "Region": "NAMER",
    "Program Name": "Hill Run",
    "Tracked in MAP?": "Yes",
    "Program Manager": "",
    "Collaborator": ""
  }
]

 

 

 

 

Array2

 

 

 

[
  {
    "ID": 1,
    "Status": "",
    "Region": "NAMER",
    "Program Name": "Fun Run",
    "Tracked in MAP?": "Yes",
    "Program Manager": "",
    "Collaborator": ""
  },
  {
    "ID": 2,
    "Status": "",
    "Region": "NAMER",
    "Program Name": "Fun Run",
    "Tracked in MAP?": "Yes",
    "Program Manager": "",
    "Collaborator": ""
  },
  {
    "ID": 3,
    "Status": "",
    "Region": "NAMER",
    "Program Name": "Long Run",
    "Tracked in MAP?": "Yes",
    "Program Manager": "",
    "Collaborator": ""
  },
  {
    "ID": 4,
    "Status": "",
    "Region": "NAMER",
    "Program Name": "Long Run",
    "Tracked in MAP?": "Yes",
    "Program Manager": "",
    "Collaborator": ""
  }
]

 

 

 

 

 

Any help that this forum can provide is greatly appreciated. I've been stuck on this forever! Also, relatively new to comparing arrays.


Best,

Daniel

5 REPLIES 5
ScottShearer
Super User
Super User

@dgonzalesjr 

This post should point you in the right direction

This post is specific to comparing arrays of objects

 

 

 

 

If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Scott
Paulie78
Super User
Super User

You can do it like this:

2021-09-01_05-15-28.png

 

Blog: tachytelic.net

YouTube: https://www.youtube.com/c/PaulieM/videos

If I answered your question, please accept it as a solution 😘

Thank you @Paulie78  and @ScottShearer. So right now I have an "Apply to each" for my Array1 where I'm performing the following:

 

filter Array2 variables('Array2')['ID'] is not equal to item()['ID']. 

 

(NOTE: New_Submits is Array1 and ExistingSessions is Array 2)

 

New_Submits=Array1 and ExistingSessions=Array2New_Submits=Array1 and ExistingSessions=Array2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

However when I run my flow the filter fails with the following error message:

 

Screen Shot 2021-09-01 at 10.46.30 AM.png

 

 

 

 

 

Any ideas why this is happening?

 

Also I should point out that my schema for both arrays are the same.

Paulie78
Super User
Super User

Why do you have the apply to each? You can copy what I did and you will get the result that you’re after.

Hey @Paulie78 I've tried just the filter without the Apply to each, one thing I noticed is that the filter is just checking to see if Array2 contains the current item but I need it to compare the property 'ID'. How would you update the filter to compare ID in both arrays?

Helpful resources

Announcements
MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

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

Power automate tips 768x460 v2.png

Restore a Deleted Flow

Did you know that you could restore a deleted flow? Check out this helpful article.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Users online (1,431)