How can I ignore an element from a list when loopi

2019-04-29 23:52发布

问题:

I have an output as follows:

[{ 
   'stderr': 'error: cannot open file',
},
{ 
   'stderr': '',
}]

Jinja snipper:

{{ php_command_result.results | map(attribute='stderr') | sort | join('\r - ') }}"

Returns a trailing - at the end because stderr is empty. How can I ignore empty values?

回答1:

Did you try rejectattr?

{{ php_command_result.results | rejectattr('stderr', 'equalto', '') | map(attribute='stderr') | sort | join('\r - ') }}"