Find all values โโin the list of cards that have the key "Oranges"
But new to groovy here, but I'm trying to get all values โโfrom every card in my list of cards that have a key equal to "Oranges"
def resultSet = [
["Oranges":123456, "Apples": "none"],["Oranges":7890, "Apples": "some"]
]
def fruit = resultSet.each{
it.findAll{key, value -> key == "Oranges"}.value
}
println fruit
so for this I expect the result to be: [123456, 7890]
but I get:[[Oranges:123456, Apples:none], [Oranges:7890, Apples:some]]
+3
source to share
1 answer
The method return each
is the collection itself. You want to apply some sort of filter to the collection, not each of its elements. I suggest using findResults
, as it will filter out nulls and nulls:
def resultSet = [
["Oranges":123456, "Apples": "none"],
["Oranges":7890, "Apples": "some"]
]
def fruit = resultSet.findResults { it.Oranges?.value }
assert fruit == [123456, 7890]
+5
source to share