Typst Fumadocs

Array

Documentation for the Array type.

A sequence of values.

You can construct an array by enclosing a comma-separated sequence of values in parentheses. The values do not have to be of the same type.

You can access and update array items with the .at() method. Indices are zero-based and negative indices wrap around to the end of the array. You can iterate over an array using a for loop. Arrays can be added together with the + operator, joined together and multiplied with integers.

Note: An array of length one needs a trailing comma, as in (1,). This is to disambiguate from a simple parenthesized expressions like (1 + 2) * 3. An empty array is written as ().

Example

Loading compiler...

Constructor

Converts a value to an array.

Note that this function is only intended for conversion of a collection-like value to an array, not for creation of an array from individual items. Use the array syntax (1, 2, 3) (or (1,) for a single-element array) instead.

Loading compiler...
#array(
  value
) -> array

Parameters

Prop

Type

Methods

The number of values in the array.

Returns the first item in the array. May be used on the left-hand side an assignment. Returns the default value if the array is empty or fails with an error is no default value was specified.

#array.first(
  default: any
) -> any

Parameters

Prop

Type

Returns the last item in the array. May be used on the left-hand side of an assignment. Returns the default value if the array is empty or fails with an error is no default value was specified.

#array.last(
  default: any
) -> any

Parameters

Prop

Type

Returns the item at the specified index in the array. May be used on the left-hand side of an assignment. Returns the default value if the index is out of bounds or fails with an error if no default value was specified.

#array.at(
  index,
  default: any
) -> any

Parameters

Prop

Type

Adds a value to the end of the array.

#array.push(
  value
) -> 

Parameters

Prop

Type

Removes the last item from the array and returns it. Fails with an error if the array is empty.

Inserts a value into the array at the specified index, shifting all subsequent elements to the right. Fails with an error if the index is out of bounds.

To replace an element of an array, use at.

#array.insert(
  index,
  value
) -> 

Parameters

Prop

Type

Removes the value at the specified index from the array and return it.

#array.remove(
  index,
  default: any
) -> any

Parameters

Prop

Type

Extracts a subslice of the array. Fails with an error if the start or end index is out of bounds.

#array.slice(
  start,
  end,
  count: int
) -> array

Parameters

Prop

Type

Whether the array contains the specified value.

This method also has dedicated syntax: You can write 2 in (1, 2, 3) instead of (1, 2, 3).contains(2).

#array.contains(
  value
) -> bool

Parameters

Prop

Type

Searches for an item for which the given function returns true and returns the first match or none if there is no match.

#array.find(
  searcher
) -> any none

Parameters

Prop

Type

Searches for an item for which the given function returns true and returns the index of the first match or none if there is no match.

#array.position(
  searcher
) -> none int

Parameters

Prop

Type

Create an array consisting of a sequence of numbers.

If you pass just one positional parameter, it is interpreted as the end of the range. If you pass two, they describe the start and end of the range.

This function is available both in the array function's scope and globally.

Loading compiler...
#array.range(
  start,
  end,
  step: int
) -> array

Parameters

Prop

Type

Produces a new array with only the items from the original one for which the given function returns true.

#array.filter(
  test
) -> array

Parameters

Prop

Type

Produces a new array in which all items from the original one were transformed with the given function.

#array.map(
  mapper
) -> array

Parameters

Prop

Type

Returns a new array with the values alongside their indices.

The returned array consists of (index, value) pairs in the form of length-2 arrays. These can be destructured with a let binding or for loop.

Loading compiler...
#array.enumerate(
  start: int
) -> array

Parameters

Prop

Type

Zips the array with other arrays.

Returns an array of arrays, where the ith inner array contains all the ith elements from each original array.

If the arrays to be zipped have different lengths, they are zipped up to the last element of the shortest array and all remaining elements are ignored.

This function is variadic, meaning that you can zip multiple arrays together at once: (1, 2).zip(("A", "B"), (10, 20)) yields ((1, "A", 10), (2, "B", 20)).

#array.zip(
  exact: bool,
  others
) -> array

Parameters

Prop

Type

Folds all items into a single value using an accumulator function.

Loading compiler...
#array.fold(
  init,
  folder
) -> any

Parameters

Prop

Type

Sums all items (works for all types that can be added).

#array.sum(
  default: any
) -> any

Parameters

Prop

Type

Calculates the product of all items (works for all types that can be multiplied).

#array.product(
  default: any
) -> any

Parameters

Prop

Type

Whether the given function returns true for any item in the array.

#array.any(
  test
) -> bool

Parameters

Prop

Type

Whether the given function returns true for all items in the array.

#array.all(
  test
) -> bool

Parameters

Prop

Type

Combine all nested arrays into a single flat one.

Return a new array with the same items, but in reverse order.

Split the array at occurrences of the specified value.

Loading compiler...
#array.split(
  at
) -> array

Parameters

Prop

Type

Combine all items in the array into one.

#array.join(
  separator,
  last: any,
  default: any | none
) -> any

Parameters

Prop

Type

Returns an array with a copy of the separator value placed between adjacent elements.

Loading compiler...
#array.intersperse(
  separator
) -> array

Parameters

Prop

Type

Splits an array into non-overlapping chunks, starting at the beginning, ending with a single remainder chunk.

All chunks but the last have chunk-size elements. If exact is set to true, the remainder is dropped if it contains less than chunk-size elements.

Loading compiler...
#array.chunks(
  chunk-size,
  exact: bool
) -> array

Parameters

Prop

Type

Returns sliding windows of window-size elements over an array.

If the array length is less than window-size, this will return an empty array.

Loading compiler...
#array.windows(
  window-size
) -> array

Parameters

Prop

Type

Return a sorted version of this array, optionally by a given key function. The sorting algorithm used is stable.

Returns an error if a pair of values selected for comparison could not be compared, or if the key or comparison function (if given) yield an error.

To sort according to multiple criteria at once, e.g. in case of equality between some criteria, the key function can return an array. The results are in lexicographic order.

Loading compiler...
#array.sorted(
  key: function,
  by: function
) -> array

Parameters

Prop

Type

Deduplicates all items in the array.

Returns a new array with all duplicate items removed. Only the first element of each duplicate is kept.

Loading compiler...
#array.dedup(
  key: function
) -> array

Parameters

Prop

Type

Converts an array of pairs into a dictionary. The first value of each pair is the key, the second the value.

If the same key occurs multiple times, the last value is selected.

Loading compiler...

Reduces the elements to a single one, by repeatedly applying a reducing operation.

If the array is empty, returns none, otherwise, returns the result of the reduction.

The reducing function is a closure with two arguments: an "accumulator", and an element.

For arrays with at least one element, this is the same as array.fold with the first element of the array as the initial accumulator value, folding every subsequent element into it.

Loading compiler...
#array.reduce(
  reducer
) -> any

Parameters

Prop

Type

On this page