How can I enumerate a hashtable as key-value pairs / filter a hashtable by a collection of key values

前端 未结 2 811
闹比i
闹比i 2020-12-17 08:33

Editor\'s note: This question has a complicated history, but boils down to this:
* To learn how to enumerate the entries of a has

2条回答
  •  孤街浪徒
    2020-12-17 09:12

    You have some options here.

    Enumerating through keys:

    foreach ($key in $var.Keys) {
        $value = $var[$key]
        # or
        $value = $var.$key 
    }
    

    Enumerating key-value pairs (which you've discovered, but may not be using effectively):

    foreach ($kvp in $var.GetEnumerator()) {
        $key = $kvp.Key
        $val = $kvp.Value
    }
    

提交回复
热议问题