Skip to content

foreach: doc recommendation #2169

@gabrielsroka

Description

@gabrielsroka

I found the foreach example a little hard to understand. Can we have something more Hello World-ish?, eg
https://jqplay.org/s/4GNwRCpQLX

jq 'foreach .[] as $item (0; . + 1; "\(.) - \($item)")'

input: ["apple", "banana", "cherry"]

output:
"1 - apple"
"2 - banana"
"3 - cherry"

Also, i'm curious why it starts at 1, not 0 -- this seems different than a foreach loop, eg, in JavaScript or Python. Would you be able to explain (here and/or in the docs)? Is this a bug?

(speaking of JavaScript, the "s" is not capitalized in the docs)

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions