Quirrel4.41.0

types.Table.topairs

Method on every table
table.topairs(): any

Returns an array of [key, value] pairs, one per slot in the table.

examples/types/table/topairs-basic.nut
let squad = {mvp = "Aria"}
let entry = squad.topairs()[0]
println("entry =", $"[{entry[0]}, {entry[1]}]")
Output:
entry = [mvp, Aria]

Return value

A new array of 2-element arrays; each inner array is [key, value] for one slot.

Notes

Takes no arguments. The order matches the table's current iteration order, which the VM does not guarantee to be stable across runs or seeds - sort the result (for example by the key at index 0) before printing or comparing it.

Equivalent to zipping keys and values together, but in one walk of the table instead of two.

Example

examples/types/table/topairs.nut
let t = {a=1, b=2, c=3}
let pairs = t.topairs()
pairs.sort(function(x, y) { return x[0] <=> y[0] })  // sort by key
foreach (p in pairs)
  println($"{p[0]}={p[1]}")

println("pairs.len() == t.len():", pairs.len() == t.len())
Output:
a=1
b=2
c=3
pairs.len() == t.len(): true

See also

keysReturns an array of every key in the table.
valuesReturns an array of every value in the table.
eachCalls callback once for every slot in the table.
tableclass index