Sign Up
Loading...

Lists

Twidget offers a variety of commands to help you manipulate lists easily without coding. Below is a detailed explanation of each command available.

Transform a list

In an endpoint, server function or event, add a command and choose List. Use the commands below to select, reshape, order or combine items. They return a result without changing the source lists. Give the result a name in Output → Return as, then select that variable in a later command.

For example, start with a list of products that each have a name, price and category. Filter can keep affordable products, Map can return their names, and Sort can order them by price. No extra workflow loop is needed.

Working with the current item

In a condition or a value for each item, choose Variable to use item, the current list item, or index, its position starting at zero. For a list of products, item.price means the price of the product currently being checked. Earlier workflow variables are also available, so a condition can compare item.price with a previously saved budget.

These two names are local to the condition or selected value. They do not become new workflow variables. If an earlier variable has the same name, the current item or position takes its place only within that field. The source list and second list are selected from the ordinary workflow variables.

Count Matching

Count how many items satisfy a condition, without creating a second list first. Select the source list and build Match items where using item or one of its fields. For example, count products where item.price is greater than 20. An empty list returns 0.

Use the existing Size command when you want to count every item, regardless of a condition.

Average

Calculate the average of a list of numbers. For a list of products, select item.price in Value to summarize. For [10, 20, 30], the result is 20. The initial selection, item, uses each whole list value.

An empty list returns null. Every selected value must be a finite number: missing values, null and text such as "20" are errors, rather than silently becoming zero or being skipped. Filter or convert unsuitable values before summarizing. A result outside the supported number range also produces an error.

To add numbers together, use the existing Sum command. To add a field from objects, use Map to select that field, then pass the mapped numbers to Sum.

Minimum

Return the smallest numeric value, or select a numeric field such as item.price. For [10, 20, 30], the result is 10. This returns the number, rather than the whole item containing it. An empty list returns null; the same numeric-value requirements as Average apply.

Maximum

Return the largest numeric value, or select a numeric field such as item.price. For [10, 20, 30], the result is 30. This returns the number rather than the whole item. An empty list returns null; the same numeric-value requirements as Average apply.

Partition

Separate a list into two results using one condition. For example, check whether item.price is at most 20. The output is an object containing:

  • matching: all items that meet the condition.

  • not_matching: all remaining items.

Both lists keep their original order. If Return as is price_groups, use price_groups.matching and price_groups.not_matching in later commands. An empty input produces two empty lists.

Separate products priced at most 20 from the remaining products.

View larger image

Flatten

Combine nested lists into one list. With Levels to flatten set to 1, [[1, 2], [3], 4] becomes [1, 2, 3, 4]. Ordinary values, including null, stay in the result.

Each level opens one layer of nested lists. For example, [[1, [2]]] becomes [1, [2]] at depth 1 and [1, 2] at depth 2. Choose a whole number from 0 to 32; the default is 1. Zero returns an unchanged copy. Deeper nested lists stay nested when the chosen depth is reached.

Flat Map

Choose a list from each item, then combine those selected lists by one level. For example, use item.lines for a list of orders to return all their line items in order. Each selected value must be a list. Empty selected lists add nothing; a missing field or a scalar value produces an error.

Use Map when you want one value per item, or when you want to retain a separate nested list for each item. Flat Map combines those lists into a single result.

Select item.lines to combine the line-item lists into one result.

View larger image

Chunk

Split a list into consecutive groups. With Items per chunk set to 2, [1, 2, 3, 4, 5] becomes [[1, 2], [3, 4], [5]]. This is useful for preparing batches for a later loop. The last group may contain fewer items.

Choose a positive whole number; the default is 50. Zero, negative numbers and fractional sizes are invalid. An empty input produces an empty list.

Take

Return the first Number of items from a list. Taking 2 from [10, 20, 30] returns [10, 20]. Zero returns an empty list; a count larger than the list returns all items. The default count is 10.

Slice

Return a range from a list using Start index and End index. Positions start at zero. The start is included and the end is excluded: slicing [10, 20, 30, 40] from 1 to 3 returns [20, 30].

Negative indexes count from the end. For the same list, start -2 and end 4 returns [30, 40]. Indexes beyond the ends are limited to the list boundaries. A range whose end comes before its start returns an empty list. Both indexes must be whole numbers; the defaults are 0 and 10.

All these commands return results without changing their source list. Counts, sizes and indexes may also come from workflow variables, but their values must meet the same requirements when the command runs.

Start at position 1 and stop before position 4 to select three items.

View larger image

Filter

Keep every item that matches a condition. Choose the source list and use the condition controls to set Match items where. For example, choose item.price, Less or Equal, and a budget of 20. The result contains the matching products in their original order. If none match, the result is an empty list.

Choose the source list and build a condition using the current item's price.

View larger image

Map

Create one result for each item. Choose a value such as item.name to make a list of product names, or choose an object to build a smaller product summary from several fields. You can also use index when building that summary. The initial selection, item, keeps each whole item; change it to the value you need. The result keeps the source order and number of items.

Build a summary with a product's name and its position in the list.

View larger image

Find

Return the first item that matches a condition. Use this when you need one product rather than all matching products. If no item matches, the result is null; add a condition in your workflow to handle that case.

Sort

Order a copy of a list. Select the value to order by, such as item.price, and choose Ascending or Descending. Use item itself for a list of numbers or text. Items with equal sort values retain their original order. Missing or null values appear last in either direction. Empty text is still a text value and sorts normally.

Use one consistent kind of value: numbers with numbers, text with text, or booleans with booleans. An incompatible mixture produces an error rather than an unpredictable order. Text sorting is case-sensitive; it does not use the member's language settings.

Select a price and choose Descending to show the highest price first.

View larger image

Distinct

Remove duplicate values while keeping the first occurrence. By default, compare each whole item. To keep one product per category, change the comparison value from item to item.category; the first product in each category is retained. Whole-object comparison checks field names and values, regardless of the order in which the fields were added.

Any

Check whether at least one item matches a condition. For example, check whether any product is out of stock. The result is true or false. An empty list returns false.

All

Check whether every item matches a condition. For example, check whether every product has a valid price. The result is true or false. An empty list returns true because there are no items that fail the condition. If an empty list should be invalid, check Not Empty as well.

Union

Combine two lists and keep one occurrence of each value. The result starts with distinct items from the first list, then adds new values from the second list. For example, [1, 2] and [2, 3] produce [1, 2, 3].

Intersection

Keep values that appear in both lists, using the first list's order and representatives. For example, [1, 2] and [2, 3] produce [2].

Difference

Keep values from the first list that do not appear in the second list. For example, [1, 2] and [2, 3] produce [1]. Swapping the lists changes the result. Unlike the existing Remove All command, Difference returns a new list and leaves the first list unchanged.

Symmetric Difference

Keep values that appear in only one list. For example, [1, 2] and [2, 3] produce [1, 3], with the first list's values before the second list's values.

Comparing two lists of objects

Union, Intersection, Difference and Symmetric Difference return distinct results. By default, they compare whole items. To match products by identity rather than every field, select item.id as the comparison value for both lists. If matching objects have different details, the first matching item is retained. Missing comparison fields count as the same comparison value, so choose a field that is present when each object must remain separate.

These commands work on lists already available to the workflow. They do not change database records. Handle invalid list inputs through the normal workflow error controls. Execution limits stop the run and cannot be caught to continue it; reduce the amount of data before transforming very large lists. Use database filtering and ordering when you only need a small selection from a large table.

Get

Description: Returns the element in the provided list at the given index.

Inputs:

  • list: The list from which to get the element.

  • index: The index of the element to return.

Output: The element at the given index in the list.

First

Description: Returns the first element of the provided list.

Inputs:

  • list: The list from which to get the first element.

Output: The first element of the list.

Last

Description: Returns the last element of the provided list.

Inputs:

  • list: The list from which to get the last element.

Output: The last element of the list.

Add

Description: Adds the provided item to the end of the provided list.

Inputs:

  • list: The list to which the item will be added.

  • item: The item to be added to the list.

Output: The number indicating the new size of the list.

Insert

Description: Inserts the provided item at the given index in the provided list.

Inputs:

  • list: The list to which the item will be inserted.

  • item: The item to be inserted.

  • index: The index at which to insert the item.

Output: The number indicating the new size of the list.

Add All

Description: Adds all of the elements in the second provided list to the end of the first provided list.

Inputs:

  • list_1: The list to which elements will be added.

  • list_2: The list containing elements to be added to list_1.

Output: None.

Remove by Index

Description: Removes the element at the given index from the provided list.

Inputs:

  • list: The list from which an item will be removed.

  • index: The index of the item to be removed.

Output: The modified list after removal.

Remove by Object

Description: Removes the specified item from the provided list if it exists.

Inputs:

  • list: The list from which an item will be removed.

  • item: The item to be removed.

Output: A boolean indicating whether an item was removed.

Remove All

Description: Removes all elements from the first list that also exist in the second list.

Inputs:

  • list_1: The list from which elements will be removed.

  • list_2: The list containing elements to be removed from list_1.

Output: None.

Index Of

Description: Returns the index of the first occurrence of the specified object in the provided list.

Inputs:

  • list: The list in which to find the item.

  • item: The item to find the index of.

Output: The index of the first occurrence of the item in the list.

Size

Description: Returns the number of elements in the provided list.

Inputs:

  • list: The list whose size will be returned.

Output: The size of the list.

Clear

Description: Removes all elements from the provided list.

Inputs:

  • list: The list to be cleared.

Output: None.

Reverse

Description: Reverses the order of the elements in the provided list.

Inputs:

  • list: The list to be reversed.

Output: A boolean indicating whether the list was successfully reversed.

Shuffle

Description: Randomly shuffles the elements in the provided list.

Inputs:

  • list: The list to be shuffled.

Output: None.

Join

Description: Concatenates all elements of the provided list into a single string using the specified delimiter.

Inputs:

  • list: The list whose elements will be concatenated.

  • delimiter: The delimiter to use between elements.

Output: A string containing all elements of the list separated by the delimiter.

Is Empty

Description: Checks whether the provided list is empty or not.

Inputs:

  • list: The list to check.

Output: A boolean indicating whether the list is empty.

Not Empty

Description: Checks whether the provided list is not empty.

Inputs:

  • list: The list to check.

Output: A boolean indicating whether the list is not empty.

Contains

Description: Checks whether the provided list contains the specified item.

Inputs:

  • list: The list in which to check.

  • item: The item to check for in the list.

Output: A boolean indicating whether the item is in the list.

Related Topics

Loops

In Twidget, loops allow you to execute commands multiple times, either based on a condition or through a set number of iterations. Loops can also be nested, meaning you can run loops within other loops, creating complex logic for your tasks. List Loop. The `List Loop` command enables you to loop thr...

Realtime commands

Use these commands in endpoints, server functions and events to connect authenticated customers and send live notifications. Start with [Realtime channels](/docs/builder/realtime) to create a channel and choose its audience. Authorize Realtime. Issue a one-use connection ticket for the signed-in cus...

Send Message

Send SMS Command. With Twidget, you can send SMS messages to phones using the `Send SMS` command. This functionality varies depending on the provider. Currently, we support Twilio as the SMS provider. To use Twilio for SMS integrations, you will need: - A Twilio account ID - Twilio Auth Token - A Tw...

Database

Twidget offers many commands to manage data in your database. Below is a list of the available commands and their functionalities: Get Data. Retrieve an entry from a table. Get Multiple Data. Retrieve multiple entries from a table. Save Data. Save an object to a database table. Save Multiple Data. S...

AI connectors

For every command’s inputs, results, examples and common errors, open the [detailed connector reference](/docs/connectors/overview). The command editor’s help button opens its exact reference section. Use an AI provider from an endpoint, server function or event. Choose your organization's connector...