def a = [[6, 4], [8, 6], [5, 3]]
a.findAll {
it.findAll {
it != 4 && it != 6
}
}
So I have this pseudo code written so I wouldn't need to write my complex code. So I want to remove all the 4's from the array but if I write only it != 4
then it doesn't remove it, if I write both of the numbers as I wrote now it != 4 && it != 6 then it removes it. Should I use it somehow differently? I want this array
def a = [[6, 4], [8, 6], [5, 3]]
To remove all number 4's from it for example.
Remove sublist with 4 inside
Use the in syntax to check inside the sublist. So your code can be rewritten as:
def a = [[6,4],[8,6],[5,3]]
assert [[8, 6], [5, 3]] == a.findAll { !(4 in it) }
Remove 4 from every sublist
def a = [[6,4],[8,6],[5,3]]
// Modify list in place, to return a new list use collect
assert [[6], [8, 6], [5, 3]] == a.each{ it.removeAll { it == 4 } }
Since you need to modify the collection you need to use collect instead of findAll:
def a = [
[6, 4],
[8, 6],
[5, 3],
]
assert a.collect { l -> l.findAll { it != 4 } } == [[6], [8, 6], [5, 3]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With