I want to find a better solution than iterating through a list of maps, which looks like this:
[[name:"Gromit", likes:"cheese", id:1234],[name:"john", likes:"fries", id:1234],...]
I have a list of names like ["lisa","carl","bob"...]. so I want to search the list of maps for the 'likes' and 'id' associated with my names list, but I don't want to iterate through the list for every single name using some boolean code.
For a regular map, I think I can do this:
if(regularMap.containsKey(key)) {
println regularmap[key]
}
How can I do something similar for a list of maps within a Jenkins declarative pipeline?
CodePudding user response:
It isn't clear what form you want the results in but the following is a starting point:
def inputData = [[name: "Gromit", likes: "cheese", id: 1234], [name: "john", likes: "fries", id: 1234]]
def names = ["lisa", "Gromit", "carl", "bob", "john",]
def results = []
names.each { name ->
def matchingMap = inputData.find { it.name == name }
if (matchingMap) {
results << [matchingMap.id, matchingMap.likes]
}
}
results
will be [[1234, cheese], [1234, fries]]
.
CodePudding user response:
listOfMaps.findAll{ it.name in names }