From fbb260159bfb8c8238daabcb4fdfec84ecbf1a40 Mon Sep 17 00:00:00 2001 From: Maciej Obarski Date: Fri, 31 Jul 2020 18:08:12 +0200 Subject: [PATCH] doc: add section with examples for `for k,v in map{}` (#6025) --- doc/docs.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/doc/docs.md b/doc/docs.md index fd91b9038e..c69c52e4ba 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -718,6 +718,30 @@ println(numbers) // [1, 2, 3] ``` When an identifier is just a single underscore, it is ignored. +#### Map `for` + +```v +m := {'one':1, 'two':2} +for key, value in m { + println("$key -> $value") // Output: one -> 1 +} // two -> 2 +``` + +Either key or value can be ignored by using a single underscore as the identifer. +```v +m := {'one':1, 'two':2} + +// iterate over keys +for key, _ in m { + println(key) // Output: one +} // two + +// iterate over values +for _, value in m { + println(value) // Output: 1 +} // 2 +``` + #### Range `for` ```v