-
Notifications
You must be signed in to change notification settings - Fork 448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
iterating through objects as key value pairs (cf. pythons .items()) #543
Comments
You can achieve that using std.objectFields or std.objectFieldsAll to get list of keys (depending on whether you want to include hidden fields). If you need both keys and values, there's no builtin function, but it's pretty simple to write yourself.
(note: I haven't tested it yet) What you describe could be a nice shortcut. @sparkprime Perhaps we should add something like |
Though it wouldn't matter for the usual small objects, implementing this as a native function would save the lookup for each key to retrieve its corresponding value ( |
You can do Iteration over objects would be nice too |
@sparkprime The syntax sugar that you gave as an example is not implemented yet, right? It throws the following exception: |
It looks like the syntax is as follows: P.S: Given that we have this syntax, why don't we close the issue? There are more than 100 open issues at the moment. 😬 |
The issue is about matching the python feature of for k, v in obj, which I think would be useful even though a simple work around is to loop over the fields doing obj[k] each time. It is indeed not implemented yet. |
I also noticed the lack of such a function, and I was surprised that Also, I noticed there's a lack of |
The order of [k , v] pairs could be a problem for stable generated configs. FWIW, Clojure implements "Sequential Interface" for Maps/Dictionaries/Objects too, the order is also not guaranteed, of course... Came here from google looking for "jsonnet iterate over object". |
sometimes it's useful to process objects by iterating through the key value pairs
python has
jq has
is there something similar in jsonnet? it seems like the list and object comprehensions could return (k,v) tuples if the right hand side is an object
The text was updated successfully, but these errors were encountered: