dictionary - 用另一张地图更新地图
问题描述
在 groovy 中,我想用另一个更新(左合并)一个地图。
def item = [attributes:[color:'Blue', weight:500], name:'hat', price:150]
def itemUpdate = [attributes:[size: 10]]
item << itemUpdate
println item
给出:
[属性:[尺寸:10],名称:帽子,价格:150]
但我想要的是:
[属性:[颜色:'蓝色',重量:500,尺寸:10],名称:'帽子',价格:150]
我也试过:
item += itemUpdate
或使用从地图更新 groovy 对象字段。没有一个能满足我的要求;在python中,方法就是update()
方法。
编辑:实际上我对 python 的看法是错误的。
解决方案
你正在做的是有效地覆盖attributes
条目。
相反,您想要做的是:
item.attributes = item.attributes + itemUpdate
你甚至可以这样做:
item.attributes += itemUpdate
两者都产生了预期
[attributes:[color:Blue, weight:500, size:10], name:hat, price:150]
推荐阅读
- azure - Azure VMSS 缩减策略不允许我为缩减规则设置超过 60 分钟的冷却时间
- python - 在 python/javascript/shell 中打开带有 Windows 64 位附件的 Outlook
- powerbi - 使用免费试用帐户将 Power BI 发布到 Web
- java - 在java中将(n)的最后一个字符更改为另一个字符
- php - 是否有更好的方法来对包含各种语言字符串的数组进行排序
- audio - ffmpeg 从 .ts 文件中仅提取 1 个音频流的一部分
- php - 如何在 PHP 中集成 csv 文件中不同列的数据?
- symfony - Shopware 6:使用 sw_include 时未加载 Twig 文件
- flutter - 如何从未来返回一个字符串
在颤振中 - docker - Docker:无法访问正在运行的服务器 api