Copying objects with shallow copy

Transcript

English (Auto-generated)

hi everyone in this cast. We'll talk about how to copy objects with a shallow copy. Starting off with iteration Using a for in loop. So in order to copy the items of one object into another object, we can iterate you through the key value pairs of an object and assign them to the target object. So let's go ahead and try this out. So I'm going to declare a variable lead counsel, one is equal to if you key value pairs also have Nintendo and then the second one can be xbox, the third one can be let's say a playstation and then the fourth keys value can be Gameboy. Okay, so got a couple of consoles here and now I'm going to declare another variable. Let a console to which is just going to be an empty object. So the intention out here is actually to copy over these key value pairs from the first object into the second object which is empty right now let me open up my console. Let me just do this. Okay, So we're going to say four each key in the console one object. I want to copy over into console too. So I want console two key to be equal to the key value pair in the console one object so we can go ahead and now check what this looks like with a console dot log statement and then print out the key value pairs in console too and there you go. So we see that all the key value pairs have been copied over from console one into console too. And if you remember from the last cast in a shallow copy, the source and target objects will share a memory address.
59 Views 0 Likes 0 Comments

How to create shallow copies.

Comment
Leave a comment (supports markdown format)