Skip to content

Commit 5676457

Browse files
authored
Mention items in Iterating over a Record (#2019)
Resolves #1987
1 parent 482ad89 commit 5676457

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

book/working_with_records.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,18 @@ To make a copy of a record with new fields, you can either:
113113

114114
## Iterating over a Record
115115

116-
You can iterate over the key-value pairs of a record by first transposing it into a table:
116+
Use the [items](/commands/docs/items.md) command to iterate over each pair of key and value:
117+
118+
```nu
119+
{ "apples": 543, "bananas": 411, "oranges": 0 } | items {|fruit, count| $"We have ($fruit) ($count)" }
120+
# => ╭───┬─────────────────────╮
121+
# => │ 0 │ We have apples 543 │
122+
# => │ 1 │ We have bananas 411 │
123+
# => │ 2 │ We have oranges 0 │
124+
# => ╰───┴─────────────────────╯
125+
```
126+
127+
Alternatively, you can [transpose](/commands/docs/transpose.md) the record into a table with columns, and then iterate over rows:
117128

118129
```nu
119130
{ "apples": 543, "bananas": 411, "oranges": 0 }

0 commit comments

Comments
 (0)