[英]How to Iterate array of objects in groovy
How to loop the array of objects in groovy?如何循环groovy中的对象数组?
I have tried below logic and got the No such property error.我尝试了以下逻辑并得到了 No such property 错误。
def map = [{asset_key = helloWorld}, {asset_key = Demo}]
for(key in map){
println(key.asset_key)
}
error:错误:
Caught: groovy.lang.MissingPropertyException: No such property: asset_key for class: maingroovy.lang.MissingPropertyException: No such property: asset_key for class: mainat main.run(main.groovy:4)捕获:groovy.lang.MissingPropertyException:没有这样的属性:class的asset_key:maingroovy.lang.MissingPropertyException:没有这样的属性:class的asset_key:mainat main.run(main.groovy:4)
expected output:预计 output:
helloWorld
Demo
The code {asset_key = helloWorld}
is not a Groovy key-value pair, it's a closure.代码{asset_key = helloWorld}
不是Groovy 键值对,它是一个闭包。 Try this instead:试试这个:
def map = [ asset_key1: 'helloWorld', asset_key2:'Demo']
println "map is: ${map}\n"
map.each { key, value ->
println "$key has value $value"
}
Running the above example will give you this result:运行上面的例子会给你这个结果:
The following code will give your desired output:以下代码将为您提供所需的 output:
def map = [ asset_key1: 'helloWorld', asset_key2:'Demo']
map.each { key, value ->
println value
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.