Thymeleaf - Check if array contains element with property

Viewed 466

I have a list of comments. Each comment has an attribute author. And each author has a username.

I want to display a specific button only if the current user did not write a comment yet. (Which means there must be no comment in the list, where the username of the author equals user.username)

This is what it would look like in JavaScript:

if(!comments.some(comment => comment.author.username === user.username))

But I have no idea, how to do it in Thymeleaf. I could not find any necessary functions like array#map(), array#any(), etc. Inline loops also don't seem to be a thing.

 <button th:if="${!<What goes here?>}" class="btn btn-primary">Add Comment</button>

How do I do it?

2 Answers

You can try this is will replicate some function of javascript

<div th:each="comment : ${comments}">
 <button th:if="${comment.author.username == user.username}" class="btn btn-primary">Add Comment</button>
</div>
Related