Skip to content

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

Photo by Tima Miroshnichenko from Pexels

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, ...)

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]

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"
}

is not equal to this:

{
   "id": "123"
}

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, ...)

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]

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)

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]

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)

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]

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.

Leave a Reply