How to filter information from arrays

Let's say we received an array with data from the server. Our task is to display information separately by certain categories.

In User State, our array looks like this:

{
  "name": "Katya",
  "names": [
    "Vasilisa",
    "Vasya",
    "Katya",
    "Yarik",
    "Jack",
    "Jake",
    "Givi",
    "Maxim",
    "Yaroslava",
    "Katya",
    "Vladimir",
    "Finn",
    "Willie",
    "Katya",
    "Casimir",
    "Ibrahim"
  ],
  "orders": [
    {
      "id": 1,
      "status": "success",
      "title": "Refrigerator",
      "price": "$54,000"
    },
    {
      "id": 2,
      "status": "fail",
      "title": "Microwave",
      "price": "$5,000"
    },
    {
      "id": 3,
      "status": "delivering",
      "title": "Kebab",
      "price": "$540"
    },
    {
      "id": 4,
      "status": "delivering",
      "title": "Pizza",
      "price": "$655"
    },
    {
      "id": 5,
      "status": "success",
      "title": "Playstation 5",
      "price": "$154,000"
    },
    {
      "id": 6,
      "status": "success",
      "title": "Loaf with fish",
      "price": "$540,000"
    }
  ]
}

So, we need the information filtered in different ways to be output to the bot.

To begin with, we will output the names. Use Message component:

1. We will output all “Katyas” from the array by copying the helper into the Message

{{filter names name}}

There are only three Katyas in our array, so the result will be like this:

Test this helper in the sandbox

2. Now we will output all Katyas in a numbered list by copying the following construction into the Message:

{{#filter names name}}
{{add @index 1}}. {{this}}
{{else}}
No such name was found
{{/filter}}

In this case, a numbered list with all Katyas will come to the bot:

Test this helper in the sandbox

3. We will find all Vasilisas in the list with such a helper and output them to the bot, in this case, we have one Vasilisa:

{{filter names "Vasilisa"}}

Result:

Test this helper in the sandbox

4. So we get all the names with a length of 4 characters:

{{filter names 4 prop="length"}}

The bot sent in order all four-letter names that are in our array:

Test this helper in the sandbox

5. Output a numbered list of all four-letter names.

To do this, add a helper to the Message:
{{#filter names 4 prop="length"}}
{{add @index 1}}. {{this}}
{{else}}
There were no such names
{{/filter}}

We will get the same persons in the bot as in the previous example, but now they are numbered:

Test this helper in the sandbox

6. Get the first two names with a length of 4 characters and output them as a list:

{{#filter names 4 prop="length" limit=2}}
{{add @index 1}}. {{this}}
{{else}}
There were no such names
{{/filter}}

Result:

Test this helper in the sandbox

7. "Rename" this and @index

(instead of index - id, instead of this - name)

Copy this construction to the Message:

{{#filter names name as |name id|}}
ID: {{id}}; Name: {{name}}
{{else}}
No such name was found
{{/filter}}

As a result, @index turned into ID and everything {{name}} output as the following list:

Test this helper in the sandbox

In addition to the names, there is information about orders in the array, which can also be output:

"orders": [
    {
      "id": 1,
      "status": "success",
      "title": "Refrigerator",
      "price": "$54,000"
    },
    {
      "id": 2,
      "status": "fail",
      "title": "Microwave",
      "price": "$5,000"
    },
    {
      "id": 3,
      "status": "delivering",
      "title": "Kebab",
      "price": "$540"
    },
    {
      "id": 4,
      "status": "delivering",
      "title": "Pizza",
      "price": "$655"
    },
    {
      "id": 5,
      "status": "success",
      "title": "Playstation 5",
      "price": "$154,000"
    },
    {
      "id": 6,
      "status": "success",
      "title": "Loaf with fish",
      "price": "$540,000"
    }
  ]

1. Output all successful orders as an array to write them to the variable:

{{JSONstringify (filter orders "success" prop="status")}}

Here's what will come to the bot:

Test this helper in the sandbox

2. We will list the orders that are delivering:

{{#filter orders "delivering" prop="status"}}
{{add @index 1}}. {{title}}
{{/filter}}

The bot sent all orders with the status "delivering"

Test this helper in the sandbox

3. Let’s Find the first Playstation 5 that comes along, determine the order number and output the price:

{{#filter orders "Playstation 5" prop="title"}}
Order number: {{id}}
Item: {{title}}
Price: {{price}}
{{else}}
You haven't bought Playstation 5 yet
{{/filter}}

Result:

Test this helper in the sandbox