I have a list of maps and I want to get a specific map on the list and return its value for a given key. I'm sure there is something fundamental I am missing, but I cannot make this work. I have several failed attempts:
void "find map by key in a list of maps"() {
given: "a list of maps"
List favorites = [
[fruit: 'apple'],
[color: 'yellow'],
[activity: 'reading']]
when:
String favoriteColor = favorites.each {
it.find { key, value ->
key == 'color'
return value
}
}
// String favoriteColor = favorites.each { it['color'] }
// String favoriteColor = favorites.find { it.key == 'color' }
// String favoriteColor = favorites.collect { it['color'] }
// String favoriteColor = favorites*.get('color')
// String favoriteColor = favorites*.get('color').value
// String favoriteColor = favorites.collect { it['color'] }.value as String
// String favoriteColor = favorites.find { it['color'] }.value as String
then:
favoriteColor == 'yellow'
}
Is there a clean way to make this work?