如何获取数组中哈希表的索引?

How to get index of hashtable in array?

我在查找数组中哈希表的索引时遇到了一些问题。我用这段代码创建了一个 JSON:

$start = {
Clear-Host
$BIB = Read-Host 'Bibliothek'
$BIBName = Read-Host 'Bibliothek Name'
$Standort = Read-Host 'Bibliothek Standort'
$Autor = Read-Host 'Buchautor'
$BuchName = Read-Host 'Buchname'

$jsonfile = "C:\Skripte\bibV2-1000.xml"
if(![System.IO.File]::Exists($jsonfile)){
    $Data = @{BIBs = @(
        @{$BIB = @{BIBName=$BIBName}, 
        @{Standort = $Standort}, 
        @{Bücher = @(
            @{BuchName = $BuchName; 
            Autor = $Autor})
        }}
    )}
    ConvertTo-Json -Depth 50 -InputObject $Data | Add-Content $jsonfile
    .$continue
} else {
        $jsonfile = "C:\Skripte\bibV2-1000.json"
        $Data = Get-Content $jsonfile | ConvertFrom-Json
        $Data.BIBs += New-Object -TypeName psobject -Property @{$BIB = 
               @{BIBname=$BIBName},
               @{Standort=$Standort},
               @{Bücher = @(@{
                    Buchname=$BuchName;
                    Autor=$Autor})
               }
        }
        ConvertTo-Json -Depth 50 -InputObject $Data | Out-File $jsonfile}
        .$continue
}


$continue = {
Write-Host ""
Write-Host "Was wollen Sie machen?"
Write-Host "(1) Eine weitere Bibliothek hinzufügen"
Write-Host "(2) Einer Bibliothek neue Bücher hinzufügen"
Write-Host "(E) Script beenden"

    If (($read = Read-Host ) -eq "1") {
    &$start} else {
            if (($read) -eq "2") {
                . C:\Skripte\büc.ps1 } else {
                    if (($read) -eq "E") {
                        exit} else {
                            Write-Host "+++ FALSCHE EINGABE! Bitte wählen Sie (1) oder (2) für die entsprechende Aktion +++"
                            .$continue
                        }
                }
    }
}
&$start

输出结果如下:

{
    "BIBs": [{
        "BIB1": [{
            "BIBName": "123"
        },
            {
                "Standort": "123"
            },
            {
                "Bücher": [{
                    "Autor": "123",
                    "BuchName": "123"
                }]
            }
        ]
    },
        {
            "BIB2": [{
                "BIBname": "345"
            },
                {
                    "Standort": "345"
                },
                {
                    "Bücher": [{
                        "Autor": "345",
                        "Buchname": "345"
                    }]
                }
            ]
        }
    ]
}

现在我想找出"BIB1"的索引。我已经尝试了应该创建输出“0”的 IndexOf()-Method,但它却给了我“-1”,因为它找不到该值。如何获取 "BIB1" 的索引?

根据您的判断earlier question, you're attempting to get the index of a specific object so you can access it via its containing array. However, you can do this more directly: $objOfInterest = $Data.BIBs | ? BIB1 - see my answer to your earlier question了解详情。

您需要遍历 $Data.BIBs 的数组元素,其中 - 在使用 ConvertFrom-Json 读取您的 serialized-to-a-file-as-JSON 哈希表时 - 是 自定义对象 (正如 Ansgar 正确指出的那样;它们是 [System.Management.Automation.PSCustomObject] 的实例),并检查每个是否存在 属性 'BIB1':

  • (在哈希表中,您将使用 .ContainsKey('BIB1') 检查是否存在 key 'BIB1'

  • 要检测一个对象是否存在属性,需要反射,这是最很容易 - 但有点模糊 - 通过隐藏的 .PSObject 属性 实现,如 .

  • 所示

但是,鉴于感兴趣的属性具有非空值,我们可以使用隐式布尔值 推断 从存在给定 属性 的非空值(逻辑):默认情况下 $obj.'BIB1' returns $null 如果没有 BIB1 属性,则在布尔上下文中为 "falsy",例如if 有条件的;相反,任何非空值都是 "truthy":

$propName = 'BIB1'
$i = $ndx = -1
foreach ($obj in $Data.BIBs) {
  ++$i
  if ($obj.$propName) { $ndx = $i; break}
}
$ndx  # $ndx now contains the index of the target object or -1 if there was no match

$Date.BIBs 是一组自定义对象,而不是哈希表(因为您将原始数据写入 JSON 文件然后将其转换回来),所以您需要这样的东西:

$arr = $Data.BIBs | ForEach-Object { $_.PSObject.Properties.Name }
$arr.IndexOf('BIB1')  # returns 0
$arr.IndexOf('BIB2')  # returns 1