yq/pkg/yqlib/doc/Has.md

49 lines
542 B
Markdown
Raw Normal View History

2020-11-24 00:38:39 +00:00
This is operation that returns true if the key exists in a map (or index in an array), false otherwise.
## Has map key
Given a sample.yml file of:
```yaml
2020-12-26 22:36:33 +00:00
- a: "yes"
'': null
2020-11-24 00:38:39 +00:00
- a: ~
2020-12-26 22:36:33 +00:00
'': null
2020-11-24 00:38:39 +00:00
- a:
2020-12-26 22:36:33 +00:00
'': null
2020-11-24 00:38:39 +00:00
- b: nope
2020-12-26 22:36:33 +00:00
'': null
2020-11-24 00:38:39 +00:00
```
then
```bash
yq eval '.[] | has("a")' sample.yml
```
will output
```yaml
true
true
true
false
```
## Has array index
Given a sample.yml file of:
```yaml
- []
- [1]
- [1, 2]
- [1, null]
- [1, 2, 3]
```
then
```bash
yq eval '.[] | has(1)' sample.yml
```
will output
```yaml
false
false
true
true
true
```