Forem

Barret Blake
Barret Blake

Posted on • Originally published at barretblake.dev on

2 2

Function Friday – Collection Functions: Intersection, Union, Skip, Take

For the second batch of collection functions we’re taking a look at the functions that let you work with the collections themselves. These functions let you slice, dice, and merge one or more collections together.

Intersection

The intersection function compares two or more collections and returns a new array that contains only the elements that exist in all the passed in collections. The format is as follows:

intersection(collection1, collection2, ...)
Enter fullscreen mode Exit fullscreen mode

As with all the functions, you can pass in collection variables or literals, and strings are treated as character arrays.

By way of example, let’s say that you have the following array variables

  • variableA is [1,3,5,7,9]
  • variableB is [1,3,4,6,7,8]
  • variableC is [2,3,7,10]

Then the output of the following function call:

intersection(variableA, variableB, variableC) // outputs [3,7]
Enter fullscreen mode Exit fullscreen mode

One thing to keep in mind is that if you are dealing with arrays of objects, all of the objects must have the same structure. What I mean by this is that this:

{
   "id": "123",
   "name": "Frank"
}
Enter fullscreen mode Exit fullscreen mode

is not equal to this:

{
   "id": "123"
}
Enter fullscreen mode Exit fullscreen mode

Because they do not have an identical structure (one has only “id” and the other has both an “id and “name” field), an intersection between two arrays that contain these will not return either.

Union

The union function returns all elements of all the passed in collections. The format is as follows:

union(collection1, collection2, ...)
Enter fullscreen mode Exit fullscreen mode

It’s important with this function to remember that the output will include all unique elements from the passed-in collections. Take the following example.

If we have the following 3 collections:

  • variableA is [1,3,5,7,9]
  • variableB is [1,3,4,6,7,8]
  • variableC is [2,3,7,10]

Then the following function call will be:

union(variableA,variableB,variableC) // output would be [1,2,3,4,5,6,7,8,9,10]
Enter fullscreen mode Exit fullscreen mode

And with our previous example of collections of objects, the output would include both objects as they are unique from each other.

Skip

The skip function will take the passed in collection, toss out the first X number of elements, then return everything after that in the output. The format is as follows:

skip(collection, skipcount)
Enter fullscreen mode Exit fullscreen mode

The skipcount value tells the function how many elements to skip before passing back the remainder.

Take the following example where we have a variable collection “variableA” that contains the following elements: [1,2,3,4,5,6,7,8,9,10].

skip(variableA, 5) // returns [6,7,8,9,10]
Enter fullscreen mode Exit fullscreen mode

Take

The take function works in the opposite manner from the skip function. It takes the X number of elements from the start of the array, then discards the rest. The pattern is as follows:

take(collection, takecount)
Enter fullscreen mode Exit fullscreen mode

The takecount parameter determines how many elements to take.

And in the following example we have a variable collection “variableA” that contains the following elements: [1,2,3,4,5,6,7,8,9,10].

take(variableA, 5) // outputs [1,2,3,4,5]
Enter fullscreen mode Exit fullscreen mode

Conclusion

These four Power Automate functions make it quite easy to work with multiple collections of like typed data, letting us compare, contrast, and manipulate the elements within them. Next week, in the final group of collection functions, we’ll take a look a manipulating the individual elements within a collection.

The post Function Friday – Collection Functions: Intersection, Union, Skip, Take first appeared on Barret Codes.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay