types.Table.topairs
Method on every
tableReturns an array of [key, value] pairs, one per slot in the table.
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
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(): trueSee also
keys | Returns an array of every key in the table. |
values | Returns an array of every value in the table. |
each | Calls callback once for every slot in the table. |
| table | class index |