Home > Enterprise >  Need to return an array element from a hashtable based upon if the key's array contains a certa
Need to return an array element from a hashtable based upon if the key's array contains a certa

Time:01-22

I have a hashtable with two keys, each key is an array. If one key's array contains a given value, I want to return the same index item from the second key. Here's the code snippet:

$ht=@{"Name" = @("Release", "Build", "Test", "CI/CD")
    "Type" = @("Release Workflow", "Build Workflow", "Testing Workflow", "CI/CD Workflow")
}



if($ht.Name.Contains("Release")){
    
}

So for the second statement, I want to return the value "Release Workflow". So far I'm not having much luck and any help would be appreciated.

CodePudding user response:

I'm not quite sure if this is what you're looking for:

$ht = @{
    "Name" = @("Release", "Build", "Test", "CI/CD")
    "Type" = @(
        "Release Workflow", "Build Workflow"
        "Testing Workflow", "CI/CD Workflow"
    )
}
$key = "Release"

# Might want to use `-contains $key` or `$key -in` here
if($ht.Name.Contains($key)){
    $ht['Type'][$ht['Name'].IndexOf($key)]
}

Note that, the .Contains(..) array method is case-sensitive, you might want to use -contains or -in operators for a case-insensitive lookup.

  •  Tags:  
  • Related