Power Automate contains excellent support for working with collections of objects. Generally, these will be treated as arrays of objects. However, these functions will also work with strings as, in Power Automate, strings are also considered a collection, as they are essentially an array of characters. As such, Power Automate contains a good selection of functions for working with these collections.
For my return to functions in Power Automate, we’ll take a look at the first group of collection functions: First, last, empty, and length.
First
The purpose of the first function is to return the first item in the collection. When these collections are created, they are static and their order does not change. The first element added to the array will always be the first element. The only way to change it is to re-define the array (the same applies to strings).
The format is as follows:
first('collection')
first([collection])
You can pass in either a static string/collection or a variable that is a string or collection. If what you pass in is a string, it will return the first character of that string. If you pass in an array of objects, it will return the first object in that array.
Examples:
first('Sweet dreams are made of this') // returns "S"
first(arrayVar) //if arrayVar is [15,25,35] it returns 15
Last
Last works in exactly the same manner as first, except that it returns the last element in the collection/string.
Format is as follows:
last('collection')
last([collection])
Again, you pass in either a static string/collection or a variable that is a string or collection.
Examples:
last('Sweet dreams are made of this') //returns "s"
last(arrayVar) //if arrayVar is [15,25,35] it returns 35
Empty
The empty function is exactly what it says: It tells you if the collection or string is empty (i.e. has no elements or length).
The format is identical to first and last:
empty('collection')
empty([collection])
As with first and last, you may pass in a literal collection, or you may pass in a variable of a string or collection. The output is a boolean value of true or false.
Examples:
empty('Sweet dreams are made of this') //returns false
empty(arrayVar) //if arrayVar is [15,25,35] it returns false
empty(arrayVar) //if arrayVar is [] it returns true
Length
Length will return the number of elements in a collection, or the number of characters if it’s a string.
The format is identical to first, last and empty:
length('collection')
length([collection])
As before, you may pass in a literal collection or you may pass in a variable of a string or collection. The output is an integer value containing the number of elements.
Examples:
length('Sweet dreams are made of this') //returns 29
length(arrayVar) //if arrayVar is [15,25,35] it returns 3
length(arrayVar) //if arrayVar is [] it returns 0
Conclusion
There we have the first few functions for working with collections. And remember that any function that works for collections will work the same way for strings, since they are essentially collections in Power Automate. Next time we’ll start walking through some of the functions that will help you work the actual elements in those collections.
The post Function Friday – Collection Functions – First, Last, Empty, Length first appeared on Barret Codes.
Top comments (0)