1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00

Allow the 'keys' function to take arrays.

This commit is contained in:
Stephen Dolan
2012-10-22 23:31:07 +01:00
parent 93f80b2726
commit 2a610da2d1
2 changed files with 12 additions and 0 deletions

View File

@@ -228,6 +228,12 @@ static void f_keys(jv input[], jv output[]) {
} }
free(keys); free(keys);
jv_free(input[0]); jv_free(input[0]);
} else if (jv_get_kind(input[0]) == JV_KIND_ARRAY) {
int n = jv_array_length(input[0]);
output[0] = jv_array();
for (int i=0; i<n; i++){
output[0] = jv_array_set(output[0], i, jv_number(i));
}
} else { } else {
output[0] = jv_invalid_with_msg(jv_string_fmt("'keys' only supports object, not %s", output[0] = jv_invalid_with_msg(jv_string_fmt("'keys' only supports object, not %s",
jv_kind_name(jv_get_kind(input[0])))); jv_kind_name(jv_get_kind(input[0]))));

View File

@@ -385,10 +385,16 @@ sections:
same for any two objects with the same set of keys, same for any two objects with the same set of keys,
regardless of locale settings. regardless of locale settings.
When `keys` is given an array, it returns the valid indices
for that array: the integers from 0 to length-1.
examples: examples:
- program: 'keys' - program: 'keys'
input: '{"abc": 1, "abcd": 2, "Foo": 3}' input: '{"abc": 1, "abcd": 2, "Foo": 3}'
output: ['["Foo", "abc", "abcd"]'] output: ['["Foo", "abc", "abcd"]']
- program: 'keys'
input: '[42,3,35]'
output: ['[0,1,2]']
- title: `select` - title: `select`
body: | body: |