groovy - 如何根据使用 groovy 的条件从列表数组中删除列表?
问题描述
我是 groovy 的新手,如果整个列表不符合条件,我需要帮助删除它这是 JSON -
{
"School" : New Elementary School,
"District" : "District1",
"City" : "NewTown",
"Students" : [ {
"Name": "Student1",
"Grade": "1"
}, {
"Name": "Student2",
"Grade": "2"
}, {
"Name": "Student3",
"Grade": "1"
}, {
"Name": "Student4",
"Grade": "1"
}, {
"Name": "Student5",
"Grade": "1"
} ],
}
我想要一个仅包含 1 年级学生的 JSON,即删除 Student2。输出应该是——
{
"School" : New Elementary School,
"District" : "District1",
"City" : "NewTown",
"Students" : [ {
"Name": "Student1",
"Grade": "1"
}, {
"Name": "Student3",
"Grade": "1"
}, {
"Name": "Student4",
"Grade": "1"
}, {
"Name": "Student5",
"Grade": "1"
} ],
}
我有适当的循环和条件。我在网上查找要删除整个列表,但似乎找不到任何东西。
解决方案
您可以使用removeAll函数来执行此操作。例子
List a = [[a:1],[a:2],[a:1]]
a.removeAll{ it.a==2 }
print(a)
[[a:1], [a:1]]
在你的情况下
students = students.removeAll{ it.Grade == "2" }
推荐阅读
- javascript - Foreach 循环和 ajax 中的 Bootstrap Collapse
- r - 如何使用ggplot2增加PCA Biolot黄色圆圈内的文本大小?
- python - Pandas - 列之间的插值
- sql - 左连接条件中的子字符串
- python - 如何编写一个 A-star 搜索算法来访问没有特定端点的所有节点?
- asp.net-core - 针对 Web API 的网站身份验证
- c# - C# HttpClient 多个获取请求同时给出 System.Net.Sockets.SocketException (10060) 异常
- c# - iOS 应用程序想要加载 Mono.Andoid
- r - R:官员;尝试在 pptx 中更改正文字体大小时出错
- android - 一个活动中同方向的两个 DrawerLayout