> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kayanos.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Array helpers

> Every registered array helper, with signatures and examples for selections, related records, totals, and ordered output.

## When to use these helpers

Use these helpers when a field or expression resolves to a list. Treat an empty list as a normal case and explicitly choose what the user should see when there are no related records.

## Calling style

Call a helper as `sum($.fee_items)`. Not every helper is available everywhere: validation, form, and document-template helpers have distinct context contracts. Editor suggestions are not proof that a helper is supported at runtime.

## Complete function reference

### `at`

Gets the element at a specific index

**Signature:** `at(arr, index)`

**Example:**

```txt theme={null}
at([1, 2, 3], 1) // 2
```

**Note:** It can also be written as a transform when the first value is the input: `value | at(...)`.

### `average`

Calculates the average of numeric values in an array or spread arguments. When the first argument is an array, it will calculate the average of that array. Otherwise, it will calculate the average of all provided arguments.

**Signature:** `average(...args)`

**Example:**

```txt theme={null}
average([1, 2, 3, 4]) // 2.5
```

**Note:** It can also be written as a transform when the first value is the input: `value | average(...)`.

### `chunk`

Chunks an array into smaller arrays of specified size

**Signature:** `chunk(arr, size)`

**Example:**

```txt theme={null}
chunk([1, 2, 3, 4, 5, 6], 2) // [[1, 2], [3, 4], [5, 6]]
```

**Note:** It can also be written as a transform when the first value is the input: `value | chunk(...)`.

### `compact`

Removes falsy values from an array

**Signature:** `compact(arr)`

**Example:**

```txt theme={null}
compact([0, 1, false, 2, "", 3]) // [1, 2, 3]
```

**Note:** It can also be written as a transform when the first value is the input: `value | compact(...)`.

### `concat`

Concatenates arrays or values. Can accept multiple arrays or a mix of arrays and values.

**Signature:** `concat(...arrays)`

**Example:**

```txt theme={null}
concat([1, 2], [3, 4]) // [1, 2, 3, 4]
```

**Note:** It can also be written as a transform when the first value is the input: `value | concat(...)`.

### `countBy`

Counts occurrences of each value in an array.

**Signature:** `countBy(arr)`

**Example:**

```txt theme={null}
countBy([1, 2, 2, 3]) // { '1': 1, '2': 2, '3': 1 }
```

**Note:** It can also be written as a transform when the first value is the input: `value | countBy(...)`.

### `difference`

Gets the difference between arrays or values. When the first argument is an array, compares against other arguments. Otherwise, treats all arguments as arrays to compare.

**Signature:** `difference(...arrays)`

**Example:**

```txt theme={null}
difference([1, 2, 3], [2, 3]) // [1]
```

**Note:** It can also be written as a transform when the first value is the input: `value | difference(...)`.

### `every`

Checks if all elements in an array are truthy.

**Signature:** `every(arr)`

**Example:**

```txt theme={null}
every([1, 2, 3]) // true
```

**Note:** It can also be written as a transform when the first value is the input: `value | every(...)`.

### `fill`

Creates an array filled with a specified value.

**Signature:** `fill(value, length)`

**Example:**

```txt theme={null}
fill(0, 5) // [0, 0, 0, 0, 0]
```

**Note:** It can also be written as a transform when the first value is the input: `value | fill(...)`.

### `first`

Gets the first element of an array

**Signature:** `first(arr)`

**Example:**

```txt theme={null}
first([1, 2, 3]) // 1
```

**Note:** It can also be written as a transform when the first value is the input: `value | first(...)`.

### `flatten`

Flattens an array by one level

**Signature:** `flatten(arr)`

**Example:**

```txt theme={null}
flatten([[1, 2], [3, 4]]) // [1, 2, 3, 4]
```

**Note:** It can also be written as a transform when the first value is the input: `value | flatten(...)`.

### `flattenDeep`

Flattens an array recursively

**Signature:** `flattenDeep(arr)`

**Example:**

```txt theme={null}
flattenDeep([[1, [2]], [3, 4]]) // [1, 2, 3, 4]
```

**Note:** It can also be written as a transform when the first value is the input: `value | flattenDeep(...)`.

### `getByPath`

Safely gets value(s) at a dot-notated path from an object or an array of objects

**Signature:** `getByPath(value, path)`

**Example:**

```txt theme={null}
getByPath({user:{name:'John'}}, 'user.name') // 'John'
```

**Note:** It can also be written as a transform when the first value is the input: `value | getByPath(...)`.

### `intersection`

Gets the intersection of arrays or values. When the first argument is an array, compares against other arguments. Otherwise, treats all arguments as arrays to compare.

**Signature:** `intersection(...arrays)`

**Example:**

```txt theme={null}
intersection([1, 2, 3], [2, 3]) // [2, 3]
```

**Note:** It can also be written as a transform when the first value is the input: `value | intersection(...)`.

### `join`

Joins an array into a string with a separator

**Signature:** `join(arr, separator?)`

**Example:**

```txt theme={null}
join([1, 2, 3], ", ") // "1, 2, 3"
```

**Note:** It can also be written as a transform when the first value is the input: `value | join(...)`.

### `last`

Gets the last element of an array

**Signature:** `last(arr)`

**Example:**

```txt theme={null}
last([1, 2, 3]) // 3
```

**Note:** It can also be written as a transform when the first value is the input: `value | last(...)`.

### `max`

Returns the largest value from an array or variadic values. This is the active array implementation after registration order is resolved.

**Signature:** `max(values) or max(...values)`

**Example:**

```txt theme={null}
max([$.inspection_fee, $.processing_fee])
```

**Availability:** General helper in shared builder and document-template runtimes; function and transform forms are available.

**Note:** It can also be written as a transform when the first value is the input: `value | max(...)`.

### `min`

Returns the smallest value from an array or variadic values. This is the active array implementation after registration order is resolved.

**Signature:** `min(values) or min(...values)`

**Example:**

```txt theme={null}
min([$.first_quote, $.second_quote])
```

**Availability:** General helper in shared builder and document-template runtimes; function and transform forms are available.

**Note:** It can also be written as a transform when the first value is the input: `value | min(...)`.

### `none`

Checks if no elements in an array are truthy.

**Signature:** `none(arr)`

**Example:**

```txt theme={null}
none([0, null, false]) // true
```

**Note:** It can also be written as a transform when the first value is the input: `value | none(...)`.

### `range`

Creates an array of numbers within a specified range.

**Signature:** `range(start, end, step?)`

**Example:**

```txt theme={null}
range(1, 5) // [1, 2, 3, 4, 5]
```

**Note:** It can also be written as a transform when the first value is the input: `value | range(...)`.

### `sample`

Gets a random element from an array

**Signature:** `sample(arr)`

**Example:**

```txt theme={null}
sample([1, 2, 3]) // 2
```

**Note:** It can also be written as a transform when the first value is the input: `value | sample(...)`.

### `sampleSize`

Gets multiple random elements from an array.

**Signature:** `sampleSize(arr, size)`

**Example:**

```txt theme={null}
sampleSize([1, 2, 3, 4], 2) // [2, 4]
```

**Note:** It can also be written as a transform when the first value is the input: `value | sampleSize(...)`.

### `shuffle`

Shuffles an array randomly

**Signature:** `shuffle(arr)`

**Example:**

```txt theme={null}
shuffle([1, 2, 3]) // [2, 1, 3]
```

**Note:** It can also be written as a transform when the first value is the input: `value | shuffle(...)`.

### `slice`

Extracts a slice of an array

**Signature:** `slice(arr, start, end?)`

**Example:**

```txt theme={null}
slice([1, 2, 3, 4, 5], 1, 3) // [2, 3]
```

**Note:** It can also be written as a transform when the first value is the input: `value | slice(...)`.

### `some`

Checks if any element in an array is truthy.

**Signature:** `some(arr)`

**Example:**

```txt theme={null}
some([0, 1, 2]) // true
```

**Note:** It can also be written as a transform when the first value is the input: `value | some(...)`.

### `sortAsc`

Sorts an array in ascending order

**Signature:** `sortAsc(arr)`

**Example:**

```txt theme={null}
sortAsc([3, 1, 4, 1, 5]) // [1, 1, 3, 4, 5]
```

**Note:** It can also be written as a transform when the first value is the input: `value | sortAsc(...)`.

### `sortBy`

Sorts array of objects or primitives by optional field and direction

**Signature:** `sortBy(arr, field?, direction?)`

**Example:**

```txt theme={null}
sortBy([{id:2},{id:1}], "id", "asc") // [{id:1},{id:2}]
```

**Note:** It can also be written as a transform when the first value is the input: `value | sortBy(...)`.

### `sortDesc`

Sorts an array in descending order

**Signature:** `sortDesc(arr)`

**Example:**

```txt theme={null}
sortDesc([3, 1, 4, 1, 5]) // [5, 4, 3, 1, 1]
```

**Note:** It can also be written as a transform when the first value is the input: `value | sortDesc(...)`.

### `sum`

Totals an array or variadic numeric values. This is the active array implementation after registration order is resolved.

**Signature:** `sum(values) or sum(...values)`

**Example:**

```txt theme={null}
sum([$.base_fee, $.inspection_fee])
```

**Availability:** General helper in shared builder and document-template runtimes; function and transform forms are available.

**Note:** It can also be written as a transform when the first value is the input: `value | sum(...)`.

### `union`

Gets the union of arrays or values (removes duplicates). Combines all arguments into a single array with unique values.

**Signature:** `union(...arrays)`

**Example:**

```txt theme={null}
union([1, 2], [2, 3]) // [1, 2, 3]
```

**Note:** It can also be written as a transform when the first value is the input: `value | union(...)`.

### `unique`

Removes duplicate values from an array

**Signature:** `unique(arr)`

**Example:**

```txt theme={null}
unique([1, 2, 2, 3, 3, 3]) // [1, 2, 3]
```

**Note:** It can also be written as a transform when the first value is the input: `value | unique(...)`.

### `zip`

Zips multiple arrays together.

**Signature:** `zip(...arrays)`

**Example:**

```txt theme={null}
zip([1, 2], [3, 4]) // [[1, 3], [2, 4]]
```

**Note:** It can also be written as a transform when the first value is the input: `value | zip(...)`.

## Before you publish

* Start with a known value and confirm the expected result.
* Handle null, empty text, and empty lists deliberately.
* Do not place secrets, access tokens, or sensitive personal data in an expression.
* Read [expression contexts and availability](/reference/expressions/contexts) before copying an expression between features.

![Array helpers in KayanOS](https://kayanos.app/docs-images/en/reference/expressions-helpers-array.png)
