From 68d13b2220b5efefffb901ddd4b34e4a1fe7376d Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sun, 5 Dec 2021 13:03:45 +1100 Subject: [PATCH] Improving docs --- operators/multiply-merge.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/operators/multiply-merge.md b/operators/multiply-merge.md index 7f83a495..61f1e975 100644 --- a/operators/multiply-merge.md +++ b/operators/multiply-merge.md @@ -234,9 +234,14 @@ will output ``` ## Merge arrays of objects together, matching on a key -There are several parts of the complex expression. -The first part is doing the hard work, it creates a map from the arrays keyed by '.a', so that there are no duplicates. -Then there's another reduce that converts that map back to an array. +The approach, at a high level, is to reduce into a merged map (keyed by the unique key) +and then convert that back into an array. + +First the expression will create a map from the arrays keyed by '.a', the unique field we want to merge by. +The reduce operator is merging '({}; . * $item )', so array elements with the matching key will be merged together. + +Next, we convert the map back to an array, using reduce again, concatenating all the map values together. + Finally, we set the result of the merged array back into the first doc. To use this, you will need to update '.myArray' to be the expression to your array (e.g. .my.array), and '.a' to be the key field of your array (e.g. '.name')