Array to map operator for #1415

This commit is contained in:
Mike Farah
2022-11-08 13:40:00 +11:00
parent c915113120
commit bd5e5dc965
8 changed files with 113 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
# Array to Map
Use this operator to convert an array to..a map. Skips over null values.
Behind the scenes, this is implemented using reduce:
```
(.[] | select(. != null) ) as $i ireduce({}; .[$i | key] = $i)
```
## Simple example
Given a sample.yml file of:
```yaml
cool:
- null
- null
- hello
```
then
```bash
yq '.cool |= array_to_map' sample.yml
```
will output
```yaml
cool:
2: hello
```
@@ -0,0 +1,9 @@
# Array to Map
Use this operator to convert an array to..a map. Skips over null values.
Behind the scenes, this is implemented using reduce:
```
(.[] | select(. != null) ) as $i ireduce({}; .[$i | key] = $i)
```