powershell Powershell函数与lodash _.merge的工作方式类似地合并Hashtables。回答问题“如何合并几个哈希表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell Powershell函数与lodash _.merge的工作方式类似地合并Hashtables。回答问题“如何合并几个哈希表相关的知识,希望对你有一定的参考价值。

<#  .SYNOPSIS
      Merges any number of hashtables into one

    .DESCRIPTION
      Merges any number of hashtables taken both from pipeline and arguments, with the hashtables in the right overwriting the keys with the same names from hastables in the left

    .EXAMPLE
      $a = @{a = 'a1'; b = 'a2'}
      $b = @{b = 'b1'; c = 'b2'}
      $c = @{c = 'c1'; d = 'c2'}

      PS> Merge-Hashtables $a $b

      Name                  Value                                                                                                                                                              
      ----                  -----
      a                     a1
      b                     b1
      c                     b2

    .EXAMPLE
      PS> $a, $b | Merge-Hashtables $c

      Name                  Value                                                                                                                                                              
      ----                  -----
      a                     a1
      b                     b1
      c                     c1
      d                     c2  
#>

Function Merge-Hashtables {
    $Result = @{}
    ($Input + $Args) | 
        Where   { ($_.Keys -ne $null) -and ($_.Values -ne $null) -and ($_.GetEnumerator -ne $null) } | 
        ForEach { $_.GetEnumerator() } | 
        ForEach { $Result[$_.Key] = $_.Value } 
    $Result
}

以上是关于powershell Powershell函数与lodash _.merge的工作方式类似地合并Hashtables。回答问题“如何合并几个哈希表的主要内容,如果未能解决你的问题,请参考以下文章