README: Array index as key for ForEach

This commit is contained in:
Michel Couillard 2023-11-22 09:36:05 -05:00
parent 6ee9f877d6
commit b877ac2b1c
1 changed files with 3 additions and 2 deletions

View File

@ -338,13 +338,14 @@ println(name.String()) // prints "Elliotte"
The `ForEach` function allows for quickly iterating through an object or array. The `ForEach` function allows for quickly iterating through an object or array.
The key and value are passed to the iterator function for objects. 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. Returning `false` from an iterator will stop iteration.
```go ```go
result := gjson.Get(json, "programmers") result := gjson.Get(json, "programmers")
result.ForEach(func(key, value gjson.Result) bool { 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 return true // keep iterating
}) })
``` ```