Welcome › Forums › General PowerShell Q&A › Renam or Merge Array Properties
- This topic has 4 replies, 3 voices, and was last updated 10 months, 3 weeks ago by
Participant.
-
AuthorPosts
-
-
March 7, 2020 at 11:08 am #208977
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:erui5746Groups : {0, 1}
Success : True
Name : 0
Captures : {0}
Index : 248
Length : 36
Value : 800
ID:erui5745Groups : {0, 1}
Success : True
Name : 0
Captures : {0}
Index : 323
Length : 35
Value : 900
ID:erui5743In 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 10 months, 3 weeks ago by
ajtsystems76.
-
This topic was modified 10 months, 3 weeks ago by
-
March 7, 2020 at 1:25 pm #208986PowerShell1234567891011$arr2 = $arr1 | foreach {[PSCustomObject]@{Groups = $_.GroupsSuccess = $_.SuccessName = $_.NameCaptures = $_.CapturesIndex = $_.IndexLength = $_.LengthMyValue = "$($_.Value)$($_.ID)"}}
-
March 7, 2020 at 1:58 pm #208989
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.
PowerShell123456789101112131415161718192021222324252627282930@{Groups = 0, 1Success = $trueName = 0Captures = @(0)Index = 173Length = 40Value = 700ID = 'eruis5746'},@{Groups = 0, 1Success = $trueName = 0Captures = @(0)Index = 248Length = 36Value = 800ID = 'erui5745'},@{Groups = 0, 1Success = $TrueName = 0Captures = @(0)Index = 323Length = 35Value = 900ID = 'erui5743'} | Select-Object -Property Groups,Success,Name,Captures,Index,Length,@{Name = 'MyValue'; Expression = {$_.Value.toString() + $_.ID.toString()}} -
March 8, 2020 at 4:39 pm #209115
awesome, worked perfectly.
Thanks for your help!
-
March 8, 2020 at 5:34 pm #209127
awesome, worked perfectly.
I’m curious. Which approach do you mean? They should work both. 😉
-
-
AuthorPosts
- The topic ‘Renam or Merge Array Properties’ is closed to new replies.