我需要在脚本里创建一个 map 的浅复制,之后就能修改其中一个键。展开运算符是一种办法。但代码在 Jenkins 管道中运行,而我们组织的政策不允许使用这个运算符。
调用 clone() 方法可以成功,不过 Intellij 会提示 "Access to 'clone' exceeds its access rights"。部分原因是 map 来自 Jenkins readJSON,它可能返回一个类似 map 的对象,而这个对象本身没有实现 clone() 方法。
排除展开运算符后,浅复制 map 最常用的方式是什么?
从英语翻译·查看原文I need to create a shallow duplicate of a map inside my script, allowing me to change one of its keys afterwards. The spread operator provides one approach. But this runs inside a Jenkins pipeline, and our organization's policy disallows that operator.
Calling the clone() method succeeds, though Intellij reports "Access to 'clone' exceeds its access rights". This is partly due to the map having been created through Jenkins readJSON, which might supply a map-like object that doesn't implement the clone() method itself.
Which approach, excluding the spread operator, is most idiomatic for shallow map copying?