DEV Community

Barret Blake
Barret Blake

Posted on • Originally published at barretblake.dev on

Function Friday – Collection Functions: Contains, Item, Join

For the last group of collection functions, we’re taking a look at the functions that focus on the items in the collection itself.

Contains

The contains function details whether or not a collection contains a specific element. The result is a boolean value (true/false). The pattern is as follows:

contains('collection', 'value')
contains([collection], 'value')
Enter fullscreen mode Exit fullscreen mode

The exact way it works depends on what the collection is. If the collection is a string, the contains function works to find a substring of characters.

contains('The rain in Spain', 'rain') // returns true
Enter fullscreen mode Exit fullscreen mode

For a dictionary, it works to find a key value. It’s important to note that it only works for keys, and not for values.

And for any other type of array, it will search the elements for a match. Remember that for arrays of complex objects, all parts of the object must match exactly, or it will return false.

contains([1,2,3,4,5], 1) // returns true
contains([{"id": 1,"name": "Fred"}], {"id": 1}) // returns false
Enter fullscreen mode Exit fullscreen mode

Item

The item function is used when you are iterating over the items in a collection, such as during an “Apply to each” loop. In these loops, the item function returns the current element being processed, including all of its child elements and properties, so that the element can be processed within the loop. The format is simple:

item()
Enter fullscreen mode Exit fullscreen mode

If you’re inside a valid loop, that will give you the current item. Otherwise, it will return an error. If it’s valid, you can reference those elements and properties within the item. For example, in one of my flows, I iterate through a list of categories from an RSS feed blog post and convert them into hashtags for LinkedIn & Twitter using the following function call:

concat('#', replace(item(), ' ', ''))
Enter fullscreen mode Exit fullscreen mode

This function gets the current category item, removes any spaces, then slaps a hash symbol in front of it. “Real life” becomes #Reallife.

Join

The join function takes all of the elements of an array and combines them into a single string. The format is:

join([collection], 'delimiter')
Enter fullscreen mode Exit fullscreen mode

The collection is any collection that can be joined together into a string. The delimiter is a separator that will be placed between the items of the collection.

Take the following example where you have an array called arrayVar that has the value of [‘one’, ‘two’, ‘three’, ‘4’, ‘5’, ‘6’]:

join(arrayVar, ',') // returns "one,two,three,4,5,6"
Enter fullscreen mode Exit fullscreen mode

Conclusion

This wraps up the last batch of collection-based functions in Power Automate. Next time we’ll start delving into some of the data type conversion functions.

The post Function Friday – Collection Functions: Contains, Item, Join first appeared on Barret Codes.

Discussion (0)