【发布时间】:2019-03-22 15:24:55
【问题描述】:
我有一个返回给我的数组,我正在使用 foreach 逐步执行该数组,我想在哈希表中使用该数组的每个成员的一个元素,该哈希表将被转换为 json 对象。当我将元素添加到 powershell 数组对象,然后将其添加到哈希时,字符串被组合而不是添加为数组。
规格
"destinationInterclusterLifIps": [
"172.31.5.119",
"172.31.15.103"
]
实际回报:
"destinationInterclusterLifIps": [
"172.31.33.150172.31.42.41"
],
构建数组
if(!$destinationInterclusterLifIps){
$destinationInterclusterLifIps = @()
foreach($record in $interclusterIpInfo.peerInterClusterLifs){
$destinationInterclusterLifIps += $record.address
}
}
哈希表
$body = @{
destinationInterclusterLifIps = @($destinationInterclusterLifIps)
}
对于我缺少的哈希表来说,这必须是一些简单的事情
【问题讨论】:
-
PowerShell 的 auto-collection-feature-thingy 可以将其减少到仅
$interclusterIpInfo.peerInterClusterLifs.address而无需循环。 -
@JeroenMostert:不幸的是,那个东西在官方文档中没有名字;我们所拥有的最接近官方名称的是blog post that announced the feature in v3 中的成员枚举。这是我在 SO 上的答案中一直使用的名称,特别是在 this one 中,它试图全面解释该功能。
-
不知道这一点。我不是这个名字的忠实粉丝——你知道,列举各个成员听起来很模棱两可,但我想总比没有好。
-
在这两个方面都同意,@JeroenMostert。文档现在至少 describe the behavior(表面上),但没有给它一个名字,所以至少假设可以选择一个更好的名字。
-
转念一想,@JeorenMostert:想出既富有表现力又简洁的名字是很困难的,member enumeration 也不错:enumeration i> 涵盖了对集合的所有元素进行操作的方面,而 member 涵盖了每个元素上正在访问的内容。
标签: arrays string powershell type-conversion