Arrays and Collections
YouTrack scripts can work with JavaScript arrays, built-in JavaScript Sets, and the YouTrack API's own Set type. Each collection type provides different operations and behavior.
The collection type and the storage format are separate choices. Check the declared return type when reading API values and the storage contract when saving data.
Collection Types
The collection type tells you which operations are available and what guarantees apply to item order and repeated values. For example, JavaScript arrays expose length, while YouTrack API Sets expose size.
JavaScript Arrays
A JavaScript array stores an ordered sequence of values. It can contain repeated values and nested arrays. Use length, indexed access, and standard array methods.
You can create arrays in backend app modules, workflow rules, import scripts, and browser widget code. An array that you create from YouTrack entities is still a JavaScript array:
APIs can also return arrays. For example, the Import API document fields include arrays, and response.json() can return an array. These values follow their interface's declared types and do not inherit app-settings row or nesting limits.
JavaScript Sets
A built-in JavaScript Set stores unique values and iterates in insertion order. Create one with new Set() and use the standard JavaScript Set API.
Built-in JavaScript Sets and YouTrack API Sets have different interfaces.
YouTrack API Sets
The YouTrack API exposes collections through its own Set type. Examples include multi-valued custom fields, issue links, search results, entity-array settings, and multi-valued entity extension properties.
Use API Set operations such as size, get(index), and forEach(visitor). To use array methods, convert the values to an array.
Work with API Collections
The source property or operation determines an API Set's accepted values, iteration order, and supported changes. The following sections explain how these rules affect collection operations. For method signatures and return values, see the Set reference.
Iteration Order
API Set operations follow the iteration order of the source collection. The API Set type does not guarantee insertion order for every collection.
Some collections define an order. For example, issue comments are ordered by creation time. For search results, specify a sort order in the query when order matters.
The first(), last(), get(), map(), and slice() methods use that iteration order. Converting a collection to an array preserves the order of the values read during conversion.
Read Collection Values
Choose a read operation according to the value you need:
To read an element by position, use
first(),last(), orget(index).To find an element that matches a condition, use
find(predicate).To count the elements, read the
sizeproperty.
Reading size can require counting the collection. Avoid repeated size calculations for large collections such as all issues in a project.
Traverse Collection Values
API Sets support visitor functions and iterators for processing collection values:
- Visitor Function
The
forEach(visitor)method calls a function for each element with arguments(value, index, set). The index is the element's zero-based position in the current iteration.- Iterator
The
entries()andvalues()methods return an API Iterator. Both methods yield individual values. Callnext()until the result hasdone: true.
Convert to an Array
Convert API Set values when you need array methods such as filter(), sort(), or join(). An API Set itself does not provide these methods.
In YouTrack 2025.3 and later, API Sets provide two methods that return JavaScript arrays:
map(visitor)returns the result of applying the visitor to each element. The visitor receives the same(value, index, set)arguments asforEach.slice(start, end)returns a shallow copy of the selected range. Callslice()without arguments to copy the entire collection.
Array conversion creates a separate container. Adding or removing elements in the array does not change membership in the source collection. Entity objects in the array still refer to YouTrack entities, so changing an entity property can update that entity.
The map() method reads the whole collection. The slice() method traverses the collection from its beginning to select a range. It does not provide database pagination.
For example, transform user entities into an array of logins, then sort the array:
To copy the first three comments into an array, use slice():
Change Collection Membership
Mutable API collections provide add(element), delete(element), and clear(). Values must match the type accepted by the source property. Adding an existing member does not create a duplicate.
Other API collections provide read operations only. For example, use issue.addComment() to add an issue comment.
A property's read-only status describes assignment to that property. Check the property and method descriptions for supported changes to its collection or contained entities.
Track Collection Changes
For collections associated with persistent entity properties, added, removed, and isChanged describe changes in the current transaction. Accessing these properties on a standalone collection, such as search results, throws an error.
For individual property descriptions, see the Set reference.
Store Collection Values
Creating an array or a built-in JavaScript Set does not save its contents in YouTrack. Choose a storage interface according to who configures or changes the data and where the data must be available.
Each storage interface defines its own accepted values and constraints.
Persistent Configuration
Use settings for configuration values, with the schema that belongs to the app or widget:
Configuration | Interface | Array Support |
|---|---|---|
App configuration entered by administrators | App settings declared in | Primitive and ordinary object arrays are JavaScript arrays in backend scripts. Entity-array settings use API Sets. The schema defines the supported shapes and constraints. |
Configuration for a dashboard or Markdown widget | Widget configuration declared through | Array settings support YouTrack entity selections. Primitive and ordinary object array settings are unsupported. |
Persistent App Data
For data that scripts create and update, choose the interface that matches the data and its location:
Data | Interface | Collection Support |
|---|---|---|
Issue fields and relationships | YouTrack entity properties and methods | Multi-valued custom fields and issue links use typed API Sets. Use the operations supported by the corresponding property or entity. |
Custom app data changed by backend scripts | Multiple entity references use API Sets. To store a primitive array or structured data, serialize it into a string property. | |
Data kept in the user's browser | Values are strings. Serialize arrays with |
Async Call State
To pass short-lived values to a follow-up async function, use ctx.store() and ctx.load(). Stored values can be strings, numbers, booleans, null, or entity references.
Serialize arrays explicitly to preserve their structure. For entity collections, serialize the required IDs or other primitive values.