mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Improving docs
This commit is contained in:
parent
3e0da390ee
commit
68d13b2220
@ -234,9 +234,14 @@ will output
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Merge arrays of objects together, matching on a key
|
## Merge arrays of objects together, matching on a key
|
||||||
There are several parts of the complex expression.
|
The approach, at a high level, is to reduce into a merged map (keyed by the unique key)
|
||||||
The first part is doing the hard work, it creates a map from the arrays keyed by '.a', so that there are no duplicates.
|
and then convert that back into an array.
|
||||||
Then there's another reduce that converts that map back to 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.
|
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')
|
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')
|
||||||
|
Loading…
Reference in New Issue
Block a user