Array changes when object taken from array gets changed
See original GitHub issueNot sure if this is a bug or if I just don’t understand the behavior: You start with an array with objects in them. You assign a variable for one of these objects. When you change that object, the original array is changed as well.
Steps to reproduce
Example1:
# Set up an array with two objects in it
$SourceArray = @(
[PSCustomObject]@{
Name = "Value1"
property1 = "blieb"
property2 = "blaab"
property3 = "bloeb"
}
[PSCustomObject]@{
Name = "Value2"
property1 = "blieb"
property2 = "blaab"
property3 = "bloeb"
}
)
# Create a variable based on an object in the array
$IssueObject = $SourceArray[0]
# Add a member to the object
$IssueObject | Add-Member -MemberType NoteProperty -Name "ExtraProperty" -Value "blaib" -Force
# So we have the object with an extra property
$IssueObject
# Now if we call the same object from the source array,
# I wouldn't expect an extra member, as I created a new variable for the changed object
$sourcearray[0]
# Turns out the object in the variable has changed as well??
# Looking further: Let's create the variable again but add | select-object *
$IssueObject = $sourcearray[1] | Select-Object *
# Do the same thing as before
$IssueObject | Add-Member -MemberType NoteProperty -Name "ExtraProperty" -Value "blaib" -Force
$IssueObject
# Call the object form the array again
$SourceArray[1]
# And results are as expected: without the extra member.
Example2 (run as admin to reproduce):
# Collect all the services in an array
$Services = Get-Service
# Collect the status of the time service
$Services | Where-Object {$_.Name -eq "W32Time"}
# Save the Time service in a new variable
$Time = $Services | Where-Object {$_.Name -eq "W32Time"}
# Start the time service (or stop if it was already started)
$Time | Start-Service
# The time service is now running. But I did not touch the Services variable.
# So I would expect the status in the Services Variable to still be Stopped.
# But it now comes back as running...
$Services | Where-Object {$_.Name -eq "W32Time"}
Expected behavior
I would expect the array to change when I define a new variable to work with. I would also not expect a difference when Select-Object is used.
Actual behavior
Environment data
Tested on multiple devices (all Windows), on PowerShell 7.1.0, 7.2.0 as well as Windows PowerShell
Name Value
---- -----
PSVersion 7.1.0
PSEdition Core
GitCommitId 7.1.0
OS Microsoft Windows 10.0.18363
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Name Value
---- -----
PSVersion 7.2.0-preview.2
PSEdition Core
GitCommitId 7.2.0-preview.2
OS Microsoft Windows 10.0.18363
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
Array value changes if object value is changed [duplicate]
I am guessing this has something to do with how JS changes the value since it is being reference by the original array...
Read more >Force change detection inside template for an object part of ...
I would refactor the currentApp data structure so that items is separated outside of it. Then updating the items array with a spread...
Read more >Array.prototype.values() - JavaScript - MDN Web Docs
The values() method of Array instances returns a new array iterator object that iterates the value of each item in the array.
Read more >Array modifier is scaling objects for seemingly no reason
The array modifier's object offset offsets each copy by the location ... set the object's scale to 1 on all axes and the...
Read more >Change to array of objects in parent not causing updates ...
It seems the child components do not see the change in the prop and thus never change their values. Is there some trick...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree with @SeeminglyScience that OP posted what they thought was unexpected behavior, so open an issue (if one doesn’t already exist) is the right thing. I appreciate @mklement0 responding with correct information and pointers. In general, we are considering enabling GitHub Discussions for general type of discussions not specific to the code of this project, but we won’t get to that til next year.
On the internet where all you see is text and no body language, sometimes one may presume the worst, but many times I find that was not the intent (which is clear in this case by @mklement0’s response later in the thread). Overall, it looks like everything worked out and I appreciate the discussion.
Ok, few responses:
As my wondering has been cleared up I will close this issue, thank you all for your responses