Unit introduction
Hey again! Welcome to unit 4 of the “Intermediate functions” course.
In this unit, you’ll expand a bit on the report built in unit 3 using some new functions. By the end of this unit, you will know how to use the most common string and array functions to your benefit.
Of course, some cases are unique and some of these other functions will be covered in more detail in later courses. You might also need to make use of other functions, so explore away if you’d like.
Let’s get started!
Starting point
You can pick up where you left off with the last scenario, or you can use the blueprint to start.
You can download the blueprint below (select one of the two):
- zip file
- JSON code
This time, all you’ll be doing is adding a list of the overall top 3 selling stores to the final report.
You will notice the starting scenario here is much smaller than the one you had at the end of the past unit, but the output is the same. The only difference is that (almost) all functions were optimized, condensed and composed into more complex formulas. With that, all those Set variable modules from before can become one!
The template will contain a module named Generate report – single module. This will show the combination of each module from the previous unit by nesting the functions. The output of that one module is the same as the last Set variable module at the end of the scenario from unit 3 of this course. You’ll get a brief explanation of this over the next few pages.
The scenario flow is: Stores 1 (Parse JSON) → Stores 2 (Parse JSON) → Generate report – single module (Compose a string).
What you’ll be building
Let’s go through a brief overview of the scenario so you can understand the context a bit better. You’ll be helping Sharmila with the final part of the report: generating a list of the top 3 stores in all regions, based on revenue.
Merge stores
The Tools → Set variable module will merge the 2 store arrays again.
Sort stores by revenue
This module will put all the stores in order so it’s easier to find the top 3. They’ll simply be at the start of the resulting array.
Get top 3 stores
This module will cut the array to size so only the top 3 are left!
Get top stores’ names
By now, you have the top 3, but Sharmila is only worried about their names. This module will simplify the array in order to have just the names in the output of this one.
Generate final report
You have the names of the top 3 stores, now all that’s left is to turn them into a neat list and Sharmila’s report is good to go!
Generating a list of the top 3 stores
Ok, let’s get to building this scenario! Go through every step before you continue.
Starting from the template
If you’re starting from the template, it has the same result as the previous unit, but a bit different configuration. If you open Generate Report - Single Module, you can see how the functions are joined together into a massive formula directly in the module, instead of using a lot of Tools > Set variable modules.
You might remember this from the optimization tips in the previous unit. It would be better to use one big formula in a single module here for the purpose of this exercise because it will save you a lot of credits.
Next, add a new Tools > Set variable module and merge the 2 store arrays again so you have a single array. To make the output clearer, name this variable merged_stores. In Variable value, insert the merge() function, and map the arrays of the two stores separated by a ;.
merge( 20.stores ; 1.stores )
Remember to close the function with a closing parenthesis ).
For this part of the report, Sharmila wants to generate a list of the top 3 stores in terms of revenue. You already have the merged stores array, so let’s sort it by revenue next. For that, you’ll use the aptly named sort() function!
sort(array; [order]; [key])Sorts values of an array. The valid values of theorderparameter are:asc(ascending order),desc(descending order),asc ci(case insensitive ascending order) anddesc ci(case insensitive descending order). Usekeyargument to access properties inside complex objects. Use raw variable names for keys. To access nested properties, use dot notation. First item in an array is index 1.
sort( Contacts ; ; name )=Sorts an array of contacts by the ‘name’ property.
sort( Emails ; ; sender.name )=Sorts an array of emails by the ‘sender.name’ property.
Try to set up the function on your own and then we’ll check the solution together in the next step. Remember, the results should be sorted by revenue. To make it easier, you should sort them in descending order so the first one is the store with the most revenue.
The image here shows the correct mapping to sort the stores by revenue. To make the output clearer, name the variable sortedStores. In Variable value, type sort( and map merged_stores array from the previous module. To get it in the descending order, type desc. The key by which you want to sort the array is revenue; you’ll find the item under merged_stores. For the key, you need to use the raw value – hover over the item to get it. It’s important that it has to have exactly the same spelling.
sort( 31.merged_stores ; desc ; revenue )
This formula tells the module to sort the merged stores according to their revenue in the descending order, so the first one is the store with the most revenue.
The next step is to get the first 3 results in the array, the top 3 stores only. For that you can use the slice() function.
slice() allows you to put the knife to the array and cut it into a smaller array. In this case, the objective is to get the first 3 items, so you’ll slice from position 0 to position 3.
Type slice( and map the sortedStores array from the previous module. Then type ; 0 ; 3 ).
slice( 31.sortedStores ; 0 ; 3 )
Note: that the
slice()function is very similar tosubstring()butslice()is used for arrays, whilesubstring()is used for… you guessed it - strings (also known as text)!
substring(text; start; end) <text>Returns a portion of a text string between the “start” position and “end” position.
substring( Hello ; 0 ; 3 )=Hel
substring( Hello ; 1 ; 3 )=el
Let’s continue with the exercise in the next part.
Generating a list of the top 3 stores
Let’s go on with this exercise! Go through every step before you continue.
Right now, you have an array of objects, so let’s simplify this a bit and get only the names of the top 3 stores. You can do so with a function that we’ve covered quite a bit already. Do you know which one?
Try to remember which function can be used to transform an array of objects into a simple array. You will find the answer in the next step. After you map the names, all you need to do is join these values together and Sharmila’s report will be complete.
The answer is the map() function. You can use the map() function to transform an array of objects into a simple array. Use storeName as key to get the top 3 store names. For example, in a Set variable module, you would use map(34.topStores, "storeName").
Now with the array of strings, all that’s left is to join those values together and create a list. That’s what the join() function is perfect for! join() will concatenate these values one in front of the other using the separator you choose. It’s the exact opposite of split(), which you’ve seen before. Makes sense, right?
Definition:
split()converts a string into an array, andjoin()converts an array into a string.
To make a nice list, you can use a newline and a dash or a star as separators. Since the first part of the report is ready, you can just add to it. For example, in a Text module, you might use join(33.topStoresNames, newline & " - ").
Sharmila’s report is ready! You can use the Gmail/Email module again if you’d like, to send the full report to yourself. If you prefer, you can just run the scenario as it is and see the result.
Great job! Her bosses are impressed and even though she joined just a few weeks ago, they already have their eye on her for a promotion!
Element - Selectable image 3 – modal
Before you move on to how to streamline your scenario by combining functions, it’s worth mentioning functions that Sharmila didn’t get to use, but can still be very handy.
add()
The add() function is very useful when you already have an array and want to include a new item to it. Unlike merge(), that joins 2 whole arrays together, add() allows you to include a single new item to an existing array, this could be a simple value or even a collection.
A good example of where this comes in handy is when adding tags to an item. You might want to add a “Make” tag to all products that were processed by Make, so you can both see it on your e-commerce platform’s UI and to filter new records for processing.
Using it is quite simple. If you have an array [1, 2, 3] and add 4, you get [1, 2, 3, 4].
For example, if array123 is [1, 1, 2, 2, 3, 3], then add(2.array123; 4) results in [1, 1, 2, 2, 3, 3, 4].
deduplicate()
However, sometimes you might add a tag that was already there, and you might need to get rid of duplicates, or you get an error!
Well, that’s where deduplicate() comes in. As the name suggests, if there are any duplicates in an array, it removes them and leaves only unique values.
deduplicate() works both with simple and complex arrays, but it only removes values that are complete duplicates. If you use it on an array of objects, 2 objects must be totally identical for one of them to be removed.
In the example, “Make” appears twice in the first array, but is then removed by deduplicate(). This can help when you want to reprocess items, but don’t want (or can’t) have duplicate tags.
For example, if arrayTags is [Tag1, Tag2, Make, Make, Tag4], then deduplicate(arrayTags; Make) results in [Tag1, Tag2, Make, Tag4].
distinct()
distinct(array; [key])Removes duplicates inside an array. Usekeyargument to access nested properties inside complex objects. To access nested properties, use dot notation. First item in an array is index 1.
Sometimes you might want to remove objects that are not identical but share one or a few values that are the same, and you only need one of them. In that case, you can use distinct(). We won’t cover this function here, but you can explore it on your own if you think it would be handy!
If you’d like to explore add() and deduplicate() in more detail, you can check out this template.
Make sure to check the first module as well to see 2 ways to create simple arrays from thin air. Truly automagic!