mirror of
https://github.com/mikefarah/yq.git
synced 2025-02-04 16:06:26 +00:00
Improving docs
This commit is contained in:
parent
b4db4496d5
commit
c17cf82791
@ -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')
|
||||||
|
@ -29,12 +29,17 @@ var mergeArrayWithAnchors = `sample:
|
|||||||
- <<: *a
|
- <<: *a
|
||||||
`
|
`
|
||||||
|
|
||||||
var mergeArraysObjectKeysText = `There are several parts of the complex expression.
|
var mergeArraysObjectKeysText = `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' in the expression to your array (e.g. .my.array), and '.a' to be the key field of your array (e.g. '.name')
|
||||||
|
|
||||||
Thanks Kev from [stackoverflow](https://stackoverflow.com/a/70109529/1168223)
|
Thanks Kev from [stackoverflow](https://stackoverflow.com/a/70109529/1168223)
|
||||||
`
|
`
|
||||||
|
Loading…
Reference in New Issue
Block a user