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
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Users online (4,009)