提示:本教程存在过时问题,更详细教程请阅读 小蓝书Typst 中文社区导航 FAQ
注意:该中文文档为社区驱动的非官网文档,可能存在错译、漏译或过时等问题,请以官网文档 Documentation 为准,如发现错漏,也欢迎 您的贡献镜像)。Typst 非官方中文交流 QQ 群:793548390
Warning: the Chinese document is a community-driven non-official document, there may be mistranslation, omission or outdated problems, please refer to the official website documentation.
Typst 中文文档

array

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

#let values = (1, 7, 4, -3, 2)

#values.at(0) \
#(values.at(0) = 3)
#values.at(-1) \
#values.find(calc.even) \
#values.filter(calc.odd) \
#values.map(calc.abs) \
#values.rev() \
#(1, (2, 3)).flatten() \
#(("A", "B", "C")
    .join(", ", last: " and "))
Preview

构造函数
如果类型具有构造函数,可以像函数一样调用它来创建一个该类型的值。

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.

#let hi = "Hello 😃"
#array(bytes(hi))
Preview

value
bytes array version
必需参数位置参数
位置参数按顺序传入,不带名称。

The value that should be converted to an array.

定义
函数和类型可以有与其关联的定义 (成员或方法)。可以使用 "." 操作符来访问调用它们。

len

The number of values in the array.

self.len(
) -> int

first

Returns the first item in the array. May be used on the left-hand side of an assignment. Fails with an error if the array is empty.

self.first(
) -> any

last

Returns the last item in the array. May be used on the left-hand side of an assignment. Fails with an error if the array is empty.

self.last(
) -> any

at

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.

self.at() -> any

index
int
必需参数位置参数
位置参数按顺序传入,不带名称。

The index at which to retrieve the item. If negative, indexes from the back.

default
any

A default value to return if the index is out of bounds.

push

Adds a value to the end of the array.

self.push()

value
any
必需参数位置参数
位置参数按顺序传入,不带名称。

The value to insert at the end of the array.

pop

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

self.pop(
) -> any

insert

Inserts a value into the array at the specified index. Fails with an error if the index is out of bounds.

self.insert(
int, any,
)

index
int
必需参数位置参数
位置参数按顺序传入,不带名称。

The index at which to insert the item. If negative, indexes from the back.

value
any
必需参数位置参数
位置参数按顺序传入,不带名称。

The value to insert into the array.

remove

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

self.remove() -> any

index
int
必需参数位置参数
位置参数按顺序传入,不带名称。

The index at which to remove the item. If negative, indexes from the back.

default
any

A default value to return if the index is out of bounds.

slice

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

self.slice() -> array

start
int
必需参数位置参数
位置参数按顺序传入,不带名称。

The start index (inclusive). If negative, indexes from the back.

end
none int
位置参数
位置参数按顺序传入,不带名称。

The end index (exclusive). If omitted, the whole slice until the end of the array is extracted. If negative, indexes from the back.

默认:none

count
int

The number of items to extract. This is equivalent to passing start + count as the end position. Mutually exclusive with end.

contains

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).

self.contains() -> bool

value
any
必需参数位置参数
位置参数按顺序传入,不带名称。

The value to search for.

find

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

self.find() -> anynone

searcher
function
必需参数位置参数
位置参数按顺序传入,不带名称。

The function to apply to each item. Must return a boolean.

position

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.

self.position() -> noneint

searcher
function
必需参数位置参数
位置参数按顺序传入,不带名称。

The function to apply to each item. Must return a boolean.

range

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.

array.range() -> array
查看示例
#range(5) \
#range(2, 5) \
#range(20, step: 4) \
#range(21, step: 4) \
#range(5, 2, step: -1)
Preview

start
int
位置参数
位置参数按顺序传入,不带名称。

The start of the range (inclusive).

默认:0

end
int
必需参数位置参数
位置参数按顺序传入,不带名称。

The end of the range (exclusive).

step
int

The distance between the generated numbers.

默认:1

filter

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

self.filter() -> array

test
function
必需参数位置参数
位置参数按顺序传入,不带名称。

The function to apply to each item. Must return a boolean.

map

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

self.map() -> array

mapper
function
必需参数位置参数
位置参数按顺序传入,不带名称。

The function to apply to each item.

enumerate

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.

self.enumerate() -> array

start
int

The index returned for the first pair of the returned list.

默认:0

zip

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)).

self.zip() -> array

others
array
必需参数位置参数
位置参数按顺序传入,不带名称。
可变参数
可变参数可以多次传入。

The arrays to zip with.

fold

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

self.fold() -> any

init
any
必需参数位置参数
位置参数按顺序传入,不带名称。

The initial value to start with.

folder
function
必需参数位置参数
位置参数按顺序传入,不带名称。

The folding function. Must have two parameters: One for the accumulated value and one for an item.

sum

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

self.sum() -> any

default
any

What to return if the array is empty. Must be set if the array can be empty.

product

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

self.product() -> any

default
any

What to return if the array is empty. Must be set if the array can be empty.

any

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

self.any() -> bool

test
function
必需参数位置参数
位置参数按顺序传入,不带名称。

The function to apply to each item. Must return a boolean.

all

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

self.all() -> bool

test
function
必需参数位置参数
位置参数按顺序传入,不带名称。

The function to apply to each item. Must return a boolean.

flatten

Combine all nested arrays into a single flat one.

self.flatten(
) -> array

rev

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

self.rev(
) -> array

split

Split the array at occurrences of the specified value.

self.split() -> array

at
any
必需参数位置参数
位置参数按顺序传入,不带名称。

The value to split at.

join

Combine all items in the array into one.

self.join() -> any

separator
any none
位置参数
位置参数按顺序传入,不带名称。

A value to insert between each item of the array.

默认:none

last
any

An alternative separator between the last two items.

intersperse

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

self.intersperse() -> array

separator
any
必需参数位置参数
位置参数按顺序传入,不带名称。

The value that will be placed between each adjacent element.

sorted

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

Returns an error if two values could not be compared or if the key function (if given) yields an error.

self.sorted() -> array

key

If given, applies this function to the elements in the array to determine the keys to sort by.

dedup

Deduplicates all items in the array.

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

self.dedup() -> array
查看示例
#(1, 1, 2, 3, 1).dedup()
Preview

key

If given, applies this function to the elements in the array to determine the keys to deduplicate by.