yq/operators/map.md

47 lines
539 B
Markdown
Raw Normal View History

2021-12-04 03:08:16 +00:00
# Map
Maps values of an array. Use `map_values` to map values of an object.
2022-02-06 03:41:27 +00:00
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
`yq e <exp> <file>`
{% endhint %}
2021-12-04 03:08:16 +00:00
## Map array
Given a sample.yml file of:
```yaml
- 1
- 2
- 3
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'map(. + 1)' sample.yml
2021-12-04 03:08:16 +00:00
```
will output
```yaml
- 2
- 3
- 4
```
## Map object values
Given a sample.yml file of:
```yaml
a: 1
b: 2
c: 3
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'map_values(. + 1)' sample.yml
2021-12-04 03:08:16 +00:00
```
will output
```yaml
a: 2
b: 3
c: 4
```