从键列表中拉出值列表 [英] pulling the list of values from the list of keys

查看:112
本文介绍了从键列表中拉出值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个记录

firstMap = [ name1:[ value1:10, value2:'name1', value3:150, value4:20 ],
             name2:[ value1:10, value2:'name2', value3:150, value4:20 ] ]

我有一个列表,其值为name1,name2等。

I have a list where the values are name1, name2, etc.

我想根据name1为

[ name1:[ value1:10, value2:'name1', value3:150, value4:20 ]

firstMap.subMap([name1]) ,为我工作,但我有一个列表,并循环列出我需要拉值

firstMap.subMap(["name1"]), did work for me, but I have a list and by looping the list I need to pull the values

namesList.each{record ->
    newMap = firstmap.subMap(record)
}

我尝试过subMap ([offer]),subMap([offer]),subMap([offer?.stringValue()]),subMap(['offer'])等等,但是没有一个可以为我工作。 p>

I have tried subMap([offer]), subMap(["offer"]), subMap(["offer?.stringValue()"]), subMap(['offer']), etc. But none of them work for me.

推荐答案

你根本不需要子图,只要你想要一次抓住几个键,或者你需要

You don't need submap at all, that's only really useful when you want to grab a few keys at once or if you need the original key in the result

尝试:

firstMap = [ name1:[ value1:10, value2:'name1', value3:150, value4:20 ],
             name2:[ value1:10, value2:'name2', value3:150, value4:20 ] ]

def namesList = [ 'name1', 'name2' ]

namesList.each { name ->
    println firstMap[ name ]
}

或者如果您需要一个地图结果使用原始查询键:

Or if you need a Map result with the original query key:

namesList.each { name ->
    println firstMap.subMap( [ name ] )
}

或者确实:

namesList.each { name ->
    println( [ (name):firstMap[ name ] ] )
}

会给你一样的(即:用钥匙名称和我的第一个例子的值创建一个新的地图)

Would give you the same (ie: create a new map with the key name and the value of my first example)

这篇关于从键列表中拉出值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆