Limiting Data Output in a Collection

  • Updated

The limit function allows you to limit the number of results in a looped output. For more information on loops, please see this article on Loops.

Syntax

  • Use the vertical pipe | to separate all other functions and filters from limit

  • Use a colon to separate the function from the numerical value of the limit

{collectionVariable | filter:'expectedResult' | limit:numericalValue}
⚠️Use the limit function with extreme caution. If possible, always use the limit function in conjunction with other filters or functions, OR for one-use collections only. If used alone on a collection without filters or functions, it will cause unexpected behaviour in that collection the next time it appears in the document.⚠️
 

Example 1: Share Classes

Sample Dataset

Share Classes:

  • Class A Common - voting

  • Class B Common - voting

  • Class C Common - voting

  • Special - non-voting

  • Preferred - non-voting

 

Limit with filter

Input

{#shareclasses | filter:'voting':false | limit:1}{className} {/}

Meaning

In the share classes collection, filtering to show only non-voting classes, list the name of the first share class appearing in the order they were input

Output

Special

 

Limit with filter and sort

Input

{#shareclasses | filter:'voting':false | orderBy_B:'className' | limit:1}{className}{/}

Meaning

In the share classes collection, filtering to show only non-voting classes, list the name of the first share class as sorted by class name in ascending order

Output

Preferred

Example 2: Shareholdings

Sample Dataset

Shareholdings:

  • Tony Stark - 25 Common shares

  • Steve Rogers - 15 Common shares

  • Bruce Banner - 10 Common shares

  • Natasha Romanoff - 20 Common shares

  • Scott Lang - 5 Common shares

  • T’Challa - 20 Common shares

  • Thor Odinson - 5 Common shares

  • Peter Parker - 5 Common shares

  • Stephen Strange - 20 Common shares

 

Limit with filter and sort

Input

{#shareholdings | filter:'totalVotes':'!0' | orderBy_B:'!totalShares' | limit:5}{name} ({totalShares} {className} shares){/}

Meaning

In the shareholdings collection, filtering to show only voting classes, list the names of the top 5 shareholders by sorting the total shares held in descending order, and include the number and class of shares held in brackets.

NOTE: In this example, the number of shares must be sorted in descending order using ! in order to capture the most shares held.

Output

  • Tony Stark (25 Common shares)

  • Natasha Romanoff (20 Common shares)

  • T’Challa (20 Common shares)

  • Stephen Strange (20 Common shares)

  • Steve Rogers (15 Common shares)