# Arrays and Collections

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/help/youtrack/devportal/llms.txt).

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:

```JAVASCRIPT
const issues = [ctx.issue, ctx.issue];
console.log(issues.length); // 2
```

APIs can also return arrays. For example, the [Import API document fields](Import-DocumentFields.html) include arrays, and [response.json()](v1-Response.html#json) can return an array. These values follow their interface's declared types and do not inherit [app-settings row or nesting limits](app-settings.html#array-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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).

Built-in JavaScript Sets and YouTrack API Sets have different interfaces.

### YouTrack API Sets

The YouTrack API exposes collections through its own [Set](v1-Set.html) 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](#convert-to-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](v1-Set.html).

### 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](v1-search.html), 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()`, or `get(index)`.

* To find an element that matches a condition, use `find(predicate)`.

* To count the elements, read the `size` property.

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()` and `values()` methods return an API [Iterator](v1-Iterator.html). Both methods yield individual values. Call `next()` until the result has `done: 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)](v1-Set.html#map) returns the result of applying the visitor to each element. The visitor receives the same `(value, index, set)` arguments as `forEach`.

* [slice(start, end)](v1-Set.html#slice) returns a shallow copy of the selected range. Call `slice()` 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:

```JAVASCRIPT
const logins = ctx.issue.permittedUsers.map(user => user.login);
logins.sort();
```

To copy the first three comments into an array, use `slice()`:

```JAVASCRIPT
const firstThreeComments = ctx.issue.comments.slice(0, 3);
console.log(firstThreeComments.length);
```

### 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()](v1-Issue.html#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](v1-Set.html#properties).

## 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](app-settings.html) declared in `settings.json` | 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](apps-widgets.html#widget-configuration) declared through `settingsSchemaPath` | 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 | [Extension properties and app global storage](apps-extension-properties.html) | 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 | [host.storage](apps-host-api.html#storage) | Values are strings. Serialize arrays with `JSON.stringify()` and parse them after reading. The browser storage quota applies. |

### Async Call State

To pass short-lived values to a follow-up async function, use [ctx.store() and ctx.load()](async-functions.html#share-state-across-async-calls). 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.

