summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNico Williams <nico@cryptonector.com>2014-05-12 00:30:55 -0500
committerNico Williams <nico@cryptonector.com>2014-05-12 00:30:55 -0500
commitd46f5d29fa16cbb1f3cf41e9607339d1fecfb0cc (patch)
treee5599f694d29e28ac7eb6c13262d5fea06960c1a
parent022a4188944bfd12cfd0dedef61644ac49e3fa13 (diff)
parent69aa0003af6848e6ea95b5ff85ca9ef24e747db2 (diff)
Merge pull request #360 from slapresta/275-unique-by
unique_by(.foo) function
-rw-r--r--builtin.c1
-rw-r--r--docs/content/3.manual/manual.yml18
2 files changed, 19 insertions, 0 deletions
diff --git a/builtin.c b/builtin.c
index 480fb8ae..c54a47c7 100644
--- a/builtin.c
+++ b/builtin.c
@@ -664,6 +664,7 @@ static const char* const jq_builtins[] = {
"def sort_by(f): _sort_by_impl(map([f]));",
"def group_by(f): _group_by_impl(map([f]));",
"def unique: group_by(.) | map(.[0]);",
+ "def unique_by(f): group_by(f) | map(.[0]);",
"def max_by(f): _max_by_impl(map([f]));",
"def min_by(f): _min_by_impl(map([f]));",
#include "libm.h"
diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml
index c8fcb5fa..aec2ca19 100644
--- a/docs/content/3.manual/manual.yml
+++ b/docs/content/3.manual/manual.yml
@@ -931,6 +931,24 @@ sections:
input: '[1,2,5,3,5,3,1,3]'
output: ['[1,2,3,5]']
+ - title: `unique_by`
+ body: |
+
+ The `unique_by(.foo)` function takes as input an array and produces
+ an array of the same elements, in sorted order, with
+ elqements with a duplicate `.foo` field removed. Think of it as making
+ an array by taking one element out of every group produced by
+ `group_by`.
+
+ examples:
+ - program: 'unique_by(.foo)'
+ input: '[{"foo": 1, "bar": 2}, {"foo": 1, "bar": 3}, {"foo": 4, "bar": 5}]'
+ output: ['[{"foo": 1, "bar": 2}, {"foo": 4, "bar": 5}]']
+ - program: 'unique_by(length)'
+ input: '["chunky", "bacon", "kitten", "cicada", "asparagus"]'
+ output: ['["chunky", "bacon", "asparagus"]']
+
+
- title: `reverse`
body: |