Home > Net >  Remove an item from array based on condition in Jenkins
Remove an item from array based on condition in Jenkins

Time:03-07

I am trying to remove an item from an array based on a condition. If a string contains a particular substring all those items should be removed from the array. From the below array i need to remove all items those have a string '-oh-'

Below is my code

 myArray = [
            "Item1",
            "Item2",
            "Item3",
            "test-oh-test",
            "Item4",
            "demo-oh-test",
            "Item5",
            "val-oh-trial"
        ]

if(myArray.contains('-oh-')
{
    myArray.remove(/*Need the syntax here*/)
}

CodePudding user response:

This will remove all matching elements:

myArray = [
            "Item1",
            "Item2",
            "Item3",
            "test-oh-test",
            "Item4",
            "demo-oh-test",
            "Item5",
            "val-oh-trial"
        ]

myArray.removeAll {it -> it.contains("-oh-")}

assert myArray == ["Item1", "Item2", "Item3", "Item4", "Item5"]
  • Related