'Remove an item from array in groovy
The array which i have in groovy is not getting recognized. How to remove an item within an array based on condition
If this is my array
def list = [
[prerelease: 'true', surname: 'surname'],
[prerelease: 'true', surname: 'surname'],
[prerelease: 'false', surname: 'surname']
]
list.removeAll(list.findAll{it.prerelease == 'true'})
println list
I get the following output
[[prerelease:false, surname:surname]]
If my array is in this format
def list1 = [
{prerelease: 'true', surname: 'surname'},
{prerelease: 'true', surname: 'surname'},
{prerelease: 'false', surname: 'surname'}
]
Then i get the following error
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/cg/root/2646066/main.groovy: 2: unexpected token: true @ line 2, column 16.
{prerelease: 'true', surname: 'surname'},
I need to make the list1 work. Is there anyway it will work in groovy if my array is in the above format with "{" parenthesis
List1 is an output from bash hence it's in this format. I need to use this array in groovy to remove some items from array. Bash script to generate the array is called from groovy. Output of that bash script returns an array like this.
Solution 1:[1]
Idk the exact output that you want. But this code is what I understand
def list = [
[prerelease: 'true', surname: 'surname'],
[prerelease: 'true', surname: 'surname'],
[prerelease: 'false', surname: 'surname']
]
def newList = []
list.each { value ->
if(value.prerelease == 'true') {
list.remove(value.prerelease)
} else {
newList.add(value)
}
}
println newList
Output
[[prerelease: 'true', surname: 'surname'],[prerelease: 'true', surname: 'surname']]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | monami |
