From 7ec745a2f78fa4cc6e6133e8f9ff0b1a9fc9bc06 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sat, 30 Oct 2021 15:52:05 +1100 Subject: [PATCH] Added flatten doc --- SUMMARY.md | 1 + operators/flatten.md | 71 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 operators/flatten.md diff --git a/SUMMARY.md b/SUMMARY.md index 80a7686f..228993ba 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -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) diff --git a/operators/flatten.md b/operators/flatten.md new file mode 100644 index 00000000..7fc012c4 --- /dev/null +++ b/operators/flatten.md @@ -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 +``` +