Added flatten doc

This commit is contained in:
Mike Farah 2021-10-30 15:52:05 +11:00
parent e92dc5060c
commit 7ec745a2f7
2 changed files with 72 additions and 0 deletions

View File

@ -28,6 +28,7 @@
* [Env Variable Operators](operators/env-variable-operators.md)
* [Equals](operators/equals.md)
* [File Operators](operators/file-operators.md)
* [Flatten](operators/flatten.md)
* [Group By](operators/group-by.md)
* [Has](operators/has.md)
* [Keys](operators/keys.md)

71
operators/flatten.md Normal file
View File

@ -0,0 +1,71 @@
# Flatten
This recursively flattens arrays.
## Flatten
Recursively flattens all arrays
Given a sample.yml file of:
```yaml
- 1
- - 2
- - - 3
```
then
```bash
yq eval 'flatten' sample.yml
```
will output
```yaml
- 1
- 2
- 3
```
## Flatten with depth of one
Given a sample.yml file of:
```yaml
- 1
- - 2
- - - 3
```
then
```bash
yq eval 'flatten(1)' sample.yml
```
will output
```yaml
- 1
- 2
- - 3
```
## Flatten empty array
Given a sample.yml file of:
```yaml
- []
```
then
```bash
yq eval 'flatten' sample.yml
```
will output
```yaml
[]
```
## Flatten array of objects
Given a sample.yml file of:
```yaml
- foo: bar
- - foo: baz
```
then
```bash
yq eval 'flatten' sample.yml
```
will output
```yaml
- foo: bar
- foo: baz
```