Reputation: 2053
I've got a simple method that looks at the request parameters coming through from a form and displays the values in a string. This method is working just fine but when an empty value comes through from the form it shows something like this:
beef:mozzarella::milk
You can see that there's an extra ":" if the value was empty how do I remove that? For some reason checking if v.size > 0 isn't working. Any ideas?
final String[] products = ["meat", "cheese", "nuts", "dairy"]
String generateProducts() {
return request.requestParameterMap.findAll { k, v -> products.contains(k) }
.collect { k, v -> v.size() > 0 ? v[0] : ""
}.join(":")
}
Upvotes: 2
Views: 866
Reputation: 50265
Another way to handle this would be:
products.collect { request.requestParameterMap[it] }.findAll().join(':')
Thanks to Tim Yates for the tap on my wrist to make me realize that I was becoming too generic about beef, mozzarella and milk when thinking of meat, cheese and dairy. ;-) (See comments on Tim's answer to notice my blatant ignorance)
Upvotes: 3
Reputation: 171144
You could make use of subMap
:
request.requestParameterMap
.subMap(products)
.findResults { k, v -> v ?: null }
.join(':')
Upvotes: 3