Make deep copies of objects in JavaScript

Transcript

English (Auto-generated)

hi everyone. So let's go ahead and talk about making deep copies of objects. Now to do this, we can use the Jason dot string If I and Jason dot parse methods now remember that in a deep copy the source and target objects have different memory addresses and are not connected at all. Unlike the shallow copies that we made. Now, what does Jason dot string? If I method do well, this method will take a javascript object as an argument and transform it into a string. And then Jason dot parse method will parse this javascript string and return a javascript object. So let's just take a look at a an example. So let's say I have an object. So let one and it has a rural property whose value is H r. And then we have a division of property which is actually a nested object. And it has a management property with let's say some other string value. Okay, there you go. Okay, so now out here we have an object and it has a property called role. And we also have a property which is called division, which references a nested object with one key called management whose value is the string. HR recruiter. So let's make a deep copy of object one. And remember that in a deep copy, copy the source and target objects have completely different memory addresses and they're not connected at all. Therefore making a change to any one of them will not affect the copy that we make. So let let's say let me make another object. So let up chu um is equal to. So what we're going to do is we're going to use Jason dot parse method which takes as an argument, Jason dot string if I which in turn takes as an argument object one. So what's happening out here is that the object is first transformed into a string and then this string will be passed and converted into an object before being returned. So out here it's going to be transformed into a string and then this result is going to be parsed and what's returned is an object and we can confirm this just by console logging object to and there you go. So in the console you can see that we have now made a deep copy of object one which is referenced by object to and it has the role key and the division key which references in s dead key value pair. So it's a nested object. Okay, so let's make a change to object to. So let's say object to dot division and um management is equal to something else. Right, So let's just say dev Now, if I consul dot blog object to out here. Right. So what is it saying? You cannot set properties of an Okay, so I might have made management division division, the spelling is not correct. There you go. So let's just change the spelling division, silly mistake but there you go. Now you see that I made a change in object to and it's reflected, the management keys, value is dev and if I console log object one, you will not see this change reflected there. It still says hR recruiter. So we've made a deep copy where in the source and target objects have completely different memory addresses and are not connected at all. And we tried this by changing the management property in the deep clone, which is object to. Okay, so once again, just to reiterate that in a deep copy, the source and target objects have different memory addresses. So when you change any of the values primitive or object type in the source or target objects after making a deep clone using Jason dot string to find Jason dot parks methods. There's simply no effect. Now one of the drawbacks of um this method is that date the built in a date object in javascript, red checks objects and infinity cannot be used only deeply nested values which contains strings and numbers, bullion and the null data type can be used to make a deep copy and we can do another example of this. So for example, let's say I have another um you know, another object. So I'm just gonna say let peterbilt is equal to an object and out here. Company is let's see peter build motors and then I have uh type which is going to be highway. So I'm just making a regular object out here and then I have class number is eight and let's say I now have load, which is actually a key which references a nested object with a couple of um he value press so I have light, I have a medium uh and then I have let's see heavy and this is going to be clearly ton. Oops. 30 ton. Okay. And that takes care of this object. So let's see what is it saying out here, syntax error, um unexpected token. Okay, where my friend um peterbilt, the type is on highway, class number, load and um save, let's just save this and there you go. Okay, so there's no um let's see. It's still telling me there's an error. Where is the error? Oh, there you go. So again, syntax mistake didn't see that. Right, So that takes care of that. Now let's make a deep copy of this target peterbilt object. And the way we're going to do that is we will say, well let truck right away, peterbilt is a truck. So let truck copy is equal to. So what happens is first we're going to use Jason dot string If I method to convert this source object which is referenced by peter built into a string and once we get the result right of this, what's going to happen is that we're going to pass this, Right, we're going to pass this using Jason dot parse method, you could have certainly done it in two steps, I've done it in one. So when I console dot log truck copy, you will see all the key value pairs which are in the source object called peterbilt. So that's all for Jason purse and Jason dot string defined methods when making deep copies of an object.
81 Views 0 Likes 0 Comments

Deep copies are those which have different memory addresses and are not connected

Comment
Leave a comment (supports markdown format)