Skip to content

Commit

Permalink
Add jq builtin reduce_keys(f)
Browse files Browse the repository at this point in the history
reduce_keys

reduce_keys(.foo) takes as input an array of objects, and produces a single object with values added for a same key 

 Example

        jq 'reduce_keys(.)'
Input	"[{"test-a": ["a"]}, {"test-a": ["b"]}, {"test-a": ["c"]}, {"test-b": ["x"]}, {"test-b": ["y"]}]"
Output	"{"test-a":["a","b","c"], "test-b":["x","y"]}"
---
        jq 'reduce_keys(.)'
Input	"[{"test-a": 1}, {"test-a": 2}, {"test-a": 3}, {"test-b": 4}, {"test-b": 5}]"
Output	"{"test-a":6, "test-b":9}"
---
        jq 'reduce_keys(.)'
Input	"[{"test-a": "a"}, {"test-a": "b"}, {"test-a": "c"}, {"test-b": "x"}, {"test-b": "y"}]"
Output	"{"test-a":"abc", "test-b":"xy"}"
---
  • Loading branch information
XavM committed Jun 27, 2014
1 parent 3e1baf5 commit ec47794
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ static const char* const jq_builtins[] = {
"def min(f): _min_by_impl(map([f]));",
"def max_by(f): max(f);",
"def min_by(f): min(f);",
"def reduce_keys(f): reduce .[] as $item ({}; .[($item|keys[])] += $item[] );",
#include "libm.h"
"def add: reduce .[] as $x (null; . + $x);",
"def del(f): delpaths([path(f)]);",
Expand Down

0 comments on commit ec47794

Please sign in to comment.