From b877ac2b1c0bd759f207bf347ddaa83975ced534 Mon Sep 17 00:00:00 2001 From: Michel Couillard Date: Wed, 22 Nov 2023 09:36:05 -0500 Subject: [PATCH] README: Array index as key for ForEach --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 96b2e4d..cd5155c 100644 --- a/README.md +++ b/README.md @@ -338,13 +338,14 @@ println(name.String()) // prints "Elliotte" The `ForEach` function allows for quickly iterating through an object or array. The key and value are passed to the iterator function for objects. -Only the value is passed for arrays. +With an array the key is an index and the value is passed. Returning `false` from an iterator will stop iteration. ```go result := gjson.Get(json, "programmers") result.ForEach(func(key, value gjson.Result) bool { - println(value.String()) + println(key.Int()) // index of the array + println(value.String()) return true // keep iterating }) ```