1 parent
f741074267
commit
061a604a2a
3 files changed
+36
No files matched your search
@@ -724,6 +724,8 @@ static const char* const jq_builtins[] = {
|
|||||||
"def values: arrays, objects, booleans, numbers, strings;",
|
"def values: arrays, objects, booleans, numbers, strings;",
|
||||||
"def scalars: select(. == null or . == true or . == false or type == \"number\" or type == \"string\");",
|
"def scalars: select(. == null or . == true or . == false or type == \"number\" or type == \"string\");",
|
||||||
"def join(x): reduce .[] as $i (\"\"; . + (if . == \"\" then $i else x + $i end));",
|
"def join(x): reduce .[] as $i (\"\"; . + (if . == \"\" then $i else x + $i end));",
|
||||||
|
"def flatten: reduce .[] as $i ([]; if $i | type == \"array\" then . + ($i | flatten) else . + [$i] end);",
|
||||||
|
"def flatten(x): reduce .[] as $i ([]; if $i | type == \"array\" and x > 0 then . + ($i | flatten(x-1)) else . + [$i] end);",
|
||||||
};
|
};
|
||||||
#undef LIBM_DD
|
#undef LIBM_DD
|
||||||
|
|
||||||
|
|||||||
@@ -796,6 +796,28 @@ sections:
|
|||||||
input: '[]'
|
input: '[]'
|
||||||
output: ["true"]
|
output: ["true"]
|
||||||
|
|
||||||
|
- title: "`flatten`"
|
||||||
|
body: |
|
||||||
|
|
||||||
|
The filter `flatten` takes as input an array of nested arrays,
|
||||||
|
and produces a flat array in which all arrays inside the original
|
||||||
|
array have been recursively replaced by their values. You can pass
|
||||||
|
an argument to it to specify how many levels of nesting to flatten.
|
||||||
|
|
||||||
|
examples:
|
||||||
|
- program: flatten
|
||||||
|
input: '[1, [2], [[3]]]'
|
||||||
|
output: ["[1, 2, 3]"]
|
||||||
|
- program: flatten(1)
|
||||||
|
input: '[1, [2], [[3]]]'
|
||||||
|
output: ["[1, 2, [3]]"]
|
||||||
|
- program: flatten
|
||||||
|
input: '[[]]'
|
||||||
|
output: ["[]"]
|
||||||
|
- program: flatten
|
||||||
|
input: '[{"foo": "bar"}, [{"foo": "baz"}]]'
|
||||||
|
output: ['[{"foo": "bar"}, {"foo": "baz"}]']
|
||||||
|
|
||||||
- title: "`range`"
|
- title: "`range`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
|
|||||||
@@ -846,3 +846,15 @@ map([1,2][0:.])
|
|||||||
[.[]|nulls]
|
[.[]|nulls]
|
||||||
[1,2,"foo",[],[3,[]],{},true,false,null]
|
[1,2,"foo",[],[3,[]],{},true,false,null]
|
||||||
[null]
|
[null]
|
||||||
|
|
||||||
|
flatten
|
||||||
|
[0, [1], [[2]], [[[3]]]]
|
||||||
|
[0, 1, 2, 3]
|
||||||
|
|
||||||
|
flatten(2)
|
||||||
|
[0, [1], [[2]], [[[3]]]]
|
||||||
|
[0, 1, 2, [3]]
|
||||||
|
|
||||||
|
flatten(2)
|
||||||
|
[0, [1, [2]], [1, [[3], 2]]]
|
||||||
|
[0, 1, 2, 1, [3], 2]
|
||||||
Reference in new issue
Block a user