From 780525fad57d35fe40033c87db62fa656553d4a1 Mon Sep 17 00:00:00 2001 From: Jan Klass Date: Sat, 23 Aug 2025 16:42:12 +0200 Subject: [PATCH] Mention `items` in Iterating over a Record Resolves #1987 --- book/working_with_records.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/book/working_with_records.md b/book/working_with_records.md index bbbdd42e54a..c5160993a27 100644 --- a/book/working_with_records.md +++ b/book/working_with_records.md @@ -113,7 +113,18 @@ To make a copy of a record with new fields, you can either: ## Iterating over a Record -You can iterate over the key-value pairs of a record by first transposing it into a table: +Use the [items](/commands/docs/items.md) command to iterate over each pair of key and value: + +```nu +{ "apples": 543, "bananas": 411, "oranges": 0 } | items {|fruit, count| $"We have ($fruit) ($count)" } +# => ╭───┬─────────────────────╮ +# => │ 0 │ We have apples 543 │ +# => │ 1 │ We have bananas 411 │ +# => │ 2 │ We have oranges 0 │ +# => ╰───┴─────────────────────╯ +``` + +Alternatively, you can [transpose](/commands/docs/transpose.md) the record into a table with columns, and then iterate over rows: ```nu { "apples": 543, "bananas": 411, "oranges": 0 }