Renam or Merge Array Properties

Welcome Forums General PowerShell Q&A Renam or Merge Array Properties

Viewing 4 reply threads
  • Author
    Posts
    • #208977
      Participant
      Topics: 2
      Replies: 2
      Points: 27
      Rank: Member

      Good Morning from the UK.

       

      I’ve got an Array in Powershell that I’ve managed to add to using my code.  It looks like this:

      Groups : {0, 1}
      Success : True
      Name : 0
      Captures : {0}
      Index : 173
      Length : 40
      Value : 700
      ID:erui5746

      Groups : {0, 1}
      Success : True
      Name : 0
      Captures : {0}
      Index : 248
      Length : 36
      Value : 800
      ID:erui5745

      Groups : {0, 1}
      Success : True
      Name : 0
      Captures : {0}
      Index : 323
      Length : 35
      Value : 900
      ID:erui5743

       

      In my array which is called $arr1 I need the properties ‘Value’ and ‘ID’ to be under the same, new property called MyValue.

      I’ve tried (foreach $a in $arr1){$arr2+=$a} to add the properties into a new array

      But I still end up with ID and Value just in another array. I need to merge ID and Value properties into a single property.

       

      Any help would be great

      • This topic was modified 2 months, 3 weeks ago by ajtsystems76.
    • #208986
      Participant
      Topics: 12
      Replies: 524
      Points: 1,220
      Helping Hand
      Rank: Community Hero
      $arr2 = $arr1 | foreach {
          [PSCustomObject]@{
              Groups   = $_.Groups
              Success  = $_.Success
              Name     = $_.Name
              Captures = $_.Captures
              Index    = $_.Index
              Length   = $_.Length
              MyValue  = "$($_.Value)$($_.ID)"
          }   
      }
      
    • #208989
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      Id would have been helpful to know how you created this array of hastables. And please format your code and sample data and console output as code using the code tags named “PRE“. Thanks.

      @{
          Groups   = 0, 1
          Success  = $true
          Name     = 0
          Captures = @(0)
          Index    = 173
          Length   = 40
          Value    = 700
          ID       = 'eruis5746'
      }, 
      @{
          Groups   = 0, 1
          Success  = $true
          Name     = 0
          Captures = @(0)
          Index    = 248
          Length   = 36
          Value    = 800
          ID       = 'erui5745'
      }, 
      @{
          Groups   = 0, 1
          Success  = $True
          Name     = 0
          Captures = @(0)
          Index    = 323
          Length   = 35
          Value    = 900
          ID       = 'erui5743'
      } | Select-Object -Property Groups,Success,Name,Captures,Index,Length,@{Name = 'MyValue'; Expression = {$_.Value.toString() + $_.ID.toString()}}
    • #209115
      Participant
      Topics: 2
      Replies: 2
      Points: 27
      Rank: Member

      awesome, worked perfectly.

       

      Thanks for your help!

    • #209127
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      awesome, worked perfectly.

      I’m curious. Which approach do you mean? They should work both. 😉

Viewing 4 reply threads
  • You must be logged in to reply to this topic.