[英]Groovy findAll in array of arrays
I have the following array of arrays in groovy 我在groovy中有以下数组数组
def userList = [[name: user1, id:0, ip: 127.0.0.1], [name: user2, id:1, ip: 127.0.0.2], [name: user3, id:2, ip: 127.0.0.3]]
I am iterating over another list rows
and I want to extract the entries from the above list based on index. 我正在遍历另一个列表rows
并且我想根据索引从上述列表中提取条目。
rows.eachWithIndex { row, index ->
groovy.lang.Closure idMatch = { it.id == index }
def match = userList.findAll(idMatch)
println(match)
}
match is always returning empty. 匹配总是返回空。
The index value shows up correctly as 0,1,2 etc when I print it. 当我打印索引值时,它正确显示为0,1,2等。
With Groovy 2.4 and above, one approach would be to use indices
and collect()
on rows
instead of eachWithIndex
: 在Groovy 2.4及更高版本中,一种方法是rows
上使用indices
和collect()
而不是eachWithIndex
:
def userList = [
[name: 'user1', id:0, ip: '127.0.0.1'],
[name: 'user2', id:1, ip: '127.0.0.2'],
[name: 'user3', id:2, ip: '127.0.0.3']
]
def rows = ['foo', 'bar']
// Using indices
rows.indices.collect { index ->
userList.find { it.id == index }
}
// Using indexed()
rows.indexed().collect { index, item ->
userList.find { it.id == index }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.