Groovy - How to remove an element from a list based on its content?


Hamsand

I'm working on a funky script for Jira - I'm collecting a list of comments for an issue and storing the username of the last comment.

example:

import com.atlassian.jira.component.ComponentAccessor
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)
if (comments) {
comments.last().authorUser
}

Sometimes, I don't want to store the username (if it belongs to a predefined role). Basically, I want to check for comments first, then remove all comments from the list that match my criteria, then by calling last(). authorUser ends. It's just like:

comments.each {
if (it.authorUser.toString().contains(user.toString())) {    
// Here is where I'd want to remove the element from the list**
}
}
comments.last().authorUser  // then store the last element as my most recent comment. 

Reasonable? I'm new to Groovy so I totally suspect there are a lot of issues. Most of the examples I've come across involve number checking...a bit difficult.

Kesternard

You can use Collection.removeAll() : it modifies the collection by removing elements that match the passed closure condition.

comments.removeAll {
    it.authorUser.toString().contains(user.toString())
}
comments.last().authorUser

Related


How to remove an element from an array based on its value?

Learn4life I have a dataset that looks like: data_minimas=[(660, 0.0), (680, 0.0), (710, 0.0), (712, 0.0), (715, 0.0), (717, 0.0), (754, 0.0), (761, 0.0), (771, 0.0), (791, 0.0), (672, 3.2426156657036575e-09), (670, 5.621795915291507e-09), (667, 6.9994187842

How to remove an element and all its duplicates from a list?

Yaschke I need to find the largest and smallest elements from a list and remove them and their duplicates. I use maximum(list) minimum(list) Found the max and min, but don't know how to remove all of them from the list. Munch filter will do what you need remo

How to remove an element and all its duplicates from a list?

Yaschke I need to find the largest and smallest elements from a list and remove them and their duplicates. I use maximum(list) minimum(list) Found the max and min, but don't know how to remove all of them from the list. Munch filter will do what you need remo

How to remove the clicked element and its siblings from the list?

cecum I have an extension for saving important URLs. Next to each link is a bin icon to remove the URL. URLs are added manually and stored in an array. You can see the picture below. So when the bin icon is clicked, the URL next to it will be removed. My HTML

How to get element and its index from list based on condition?

Alyssa Alex I have a list with duplicate values, I found the list with max length, but I want to get the index of these max list and add it to the index list. mylist = [{'destination', 'graph'}, {'vertex'}, {'destination', 'modify'}, {'destination', 'modify'},

How to get element and its index from list based on condition?

Alyssa Alex I have a list with duplicate values, I found the list with max length, but I want to get the index of these max list and add it to the index list. mylist = [{'destination', 'graph'}, {'vertex'}, {'destination', 'modify'}, {'destination', 'modify'},

How to use javascript to remove an element but not its content?

weybrook I have a simple string <a>. var str = '<a href="somewhere.com">Link</a>' I want to become: Link I can </a>easily remove the part, str.replace("</a>", "")but how do I remove the start tag <a href="somewhere.com>? Jezlo This will strip the entire tag:

Remove a list from a list based on the nth element

Jayante I have a list of lists like: [[1, 2, 3, 2], [1, 3, 4, 3], [1, 4, 5, 4], [2, 3, 5, 6], [1, 5, 6, 5], [2, 4, 6, 8], [1, 6, 7, 6], [2, 5, 7, 10], [3, 4, 7, 12], [2, 6, 8, 12]] I want to get the last element and check if it is the same as the 4th element

Remove a list from a list based on the nth element

Jayante I have a list of lists like: [[1, 2, 3, 2], [1, 3, 4, 3], [1, 4, 5, 4], [2, 3, 5, 6], [1, 5, 6, 5], [2, 4, 6, 8], [1, 6, 7, 6], [2, 5, 7, 10], [3, 4, 7, 12], [2, 6, 8, 12]] I want to get the last element and check if it is the same as the 4th element

How to remove specific element with content based on position

Rudramuni TP For suggestions on how to remove "aaaaa" "mi" contained in the text , If found under msub's first child and last position (text node) If found under the second child of msub and the first position (text node) For a quick look, I've added comments

How to remove specific element with content based on position

Rudramuni TP For suggestions on how to remove "aaaaa" "mi" contained in the text , If found under msub's first child and last position (text node) If found under the second child of msub and the first position (text node) For a quick look, I've added comments

Remove element from list if content is the same?

Moza Salman I'm trying to remove all items from a list that contains a specific character (ie "z", "l" or "t"). Here is what I have so far: def run(): words = ['the', 'of', 'and', 'to', 'a', 'in', 'for', 'is', 'on', 'that', 'by', 'this', 'with', 'i', 'you'

How to remove duplicates from a list in Groovy

Manas Kumar: Hi I have a JIRA Groovy code. Here I need to sort or remove duplicate values from the list. E.g: ["154515 Sawgrass", "170985 Mexico APIs for Payments", "153026 CitiCards Consumer and Business Account Online (authenticated pgs), No CSI App ID",

How to remove duplicates from a list in Groovy

Manas Kumar: Hi I have a JIRA Groovy code. Here I need to sort or remove duplicate values from the list. E.g: ["154515 Sawgrass", "170985 Mexico APIs for Payments", "153026 CitiCards Consumer and Business Account Online (authenticated pgs), No CSI App ID",

remove element from python list based on filter

username I have 3 lists in Python. list_a = [10., 20., 30., 12.] list_b = [30., 20., 60., 12.] list_c = [10., 80., 90., 12.] I want to remove these elements where list_band list_cwhere the value list_ais <= 15. So the result becomes: list_b = [20., 60.] list_

Remove an element from a list based on a condition

Krupke I have the following code: from collections import defaultdict import pandas as pd THRESHOLD = 3 item_counts = defaultdict(int) df = {'col1':['1 2 3 4 5 6 7', '1 3 6 7','2 6 7']} lines = pd.DataFrame(data=df) print(lines) for line in lines['col1']

remove element from python list based on filter

username I have 3 lists in Python. list_a = [10., 20., 30., 12.] list_b = [30., 20., 60., 12.] list_c = [10., 80., 90., 12.] I want to remove these elements where list_band list_cwhere the value list_ais <= 15. So the result becomes: list_b = [20., 60.] list_

remove element from python list based on filter

username I have 3 lists in Python. list_a = [10., 20., 30., 12.] list_b = [30., 20., 60., 12.] list_c = [10., 80., 90., 12.] I want to remove these elements where list_band list_cwhere the value list_ais <= 15. So the result becomes: list_b = [20., 60.] list_

Remove element from tree based on term list

username I'm trying to capture some text from a webpage (passing its URL when running the script), but it's hidden in a paragraph tag with no other attributes assigned. I can collect the content of each paragraph tag, but I want to remove any element from the

Remove an element from a list based on a condition

Krupke I have the following code: from collections import defaultdict import pandas as pd THRESHOLD = 3 item_counts = defaultdict(int) df = {'col1':['1 2 3 4 5 6 7', '1 3 6 7','2 6 7']} lines = pd.DataFrame(data=df) print(lines) for line in lines['col1']

remove element from python list based on filter

username I have 3 lists in Python. list_a = [10., 20., 30., 12.] list_b = [30., 20., 60., 12.] list_c = [10., 80., 90., 12.] I want to remove these elements where list_band list_cwhere the value list_ais <= 15. So the result becomes: list_b = [20., 60.] list_

Remove item from list based on element UUID

Aleph I feel a little embarrassed to ask this, but after more than a day of trying, I'm stuck. Based on the replies to other questions, I made some changes to the code. The latest code is essentially selecting items in a list based on UUID. This caused my dele

Remove an element from a list based on a condition

Krupke I have the following code: from collections import defaultdict import pandas as pd THRESHOLD = 3 item_counts = defaultdict(int) df = {'col1':['1 2 3 4 5 6 7', '1 3 6 7','2 6 7']} lines = pd.DataFrame(data=df) print(lines) for line in lines['col1']

remove element from python list based on filter

username I have 3 lists in Python. list_a = [10., 20., 30., 12.] list_b = [30., 20., 60., 12.] list_c = [10., 80., 90., 12.] I want to remove these elements where list_band list_cwhere the value list_ais <= 15. So the result becomes: list_b = [20., 60.] list_

remove element from python list based on filter

username I have 3 lists in Python. list_a = [10., 20., 30., 12.] list_b = [30., 20., 60., 12.] list_c = [10., 80., 90., 12.] I want to remove these elements where list_band list_cwhere the value list_ais <= 15. So the result becomes: list_b = [20., 60.] list_