简体   繁体   English

Powershell.组合数组中对象的属性

[英]Powershell. Combine properties of objects in array

I'm not very good at powershell and I need help.我不太擅长 powershell,我需要帮助。 I am trying to combine properties of objects.我正在尝试组合对象的属性。 For two objects, everything is fine.对于两个对象,一切都很好。 This is looks like.这是长得像

$obj1 = [PSCustomObject]@{
    'ip' = '10.10.10.11'
    't1' = 'show'
    't2' = 'ab'

}
$obj2 = [PSCustomObject]@{
    'ip' = '10.10.10.11'
    't1' = 'me'
    't2' = 'cd'
} 

foreach ($prop in $obj2 | Get-Member -MemberType NoteProperty | Where-Object {$_.name -ne 'ip'} | select -ExpandProperty name)
{
    $obj1.$prop += $obj2.$prop
}

$obj1 

Result:
ip                         t1                         t2                       
--                         --                         --                       
10.10.10.11                showme                     abcd  

But I can't do this if many objects in an array.但是如果数组中有很多对象,我就不能这样做。 How to do it?怎么做? Example arrays:示例 arrays:

Array1:
ip                         t1                         t2                       
--                         --                         --                       
10.10.10.11                show                       ab                       
10.10.10.12                show                       ab   

Array2:
ip                         t1                         t2                       
--                         --                         --                       
10.10.10.11                me                         cd                       
10.10.10.12                me                         cd  

Basically, all you need is to add an outer loop that iterates over the array elements.基本上,您需要的只是添加一个迭代数组元素的外部循环。

Additionally, you can use the hidden .psobject.Properties member to streamline your solution:此外,您可以使用隐藏的.psobject.Properties成员来简化您的解决方案:

# Sample arrays
$array1 = [pscustomobject]@{ 'ip' = '10.10.10.11'; 't1' = 'show1'; 't2' = 'ab1' },
          [pscustomobject]@{ 'ip' = '10.10.10.12'; 't1' = 'show2'; 't2' = 'ab2' }

$array2 = [pscustomobject]@{ 'ip' = '10.10.10.11'; 't1' = 'me1'; 't2' = 'cd1' },
          [pscustomobject]@{ 'ip' = '10.10.10.12'; 't1' = 'me2'; 't2' = 'cd2' }

# Assuming all objects in the arrays have the same property structure,
# get the list of property names up front.
$mergeProperties = $array1[0].psobject.Properties.Name -ne 'ip'

# Loop over the array elements in pairs and merge their property values.
$i = 0
[array] $mergedArray = 
  foreach ($o1 in $array1) {
    $o2 = $array2[$i++]
    foreach ($p in $mergeProperties) {
        $o1.$p += $o2.$p
    }
    $o1  # output the merged object
  }

Outputting $mergedArray then yields:输出$mergedArray然后产生:

ip          t1       t2
--          --       --
10.10.10.11 show1me1 ab1cd1
10.10.10.12 show2me2 ab2cd2

Note:笔记:

  • As in your attempt, one of the original objects is modified in place.与您的尝试一样,原始对象之一已就地修改。

  • The assumption is that matching objects in the two arrays are in the same position, respectively (merge the first object from the first array with the first object from the second, ....).假设两个 arrays 中的匹配对象分别在同一个 position 中(将第一个数组中的第一个 object 与第二个数组中的第一个 object 合并,....)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM