Amazing!! Everyone say that an object is referred to by a memory address but no one showed it. You are showing it and also how it is happening in browser. Now this makes more sense and also easy to remember. And @40.00 you showed how one object is stored in another object, that blew my mind, it was so simple. Thanks for sharing the knowledge.
By using Json we can make deepcopy let's understand through an example: const ingredientsList = ["noodles", { list: ["eggs", "flour", "water"] }]; const ingredientsListDeepCopy = JSON.parse(JSON.stringify(ingredientsList)); JSON.stringf() converts the original element of an array into strings and then JSON.parse converts the strings elements into Object / Array by doing this the address of original array will change and this time we'll get deep copy of previous object / Array not shallow copy. may be I'm not able to make more clear to you. For more clarity you can see mdn. Thankyou Anurag sir for this Wonderful playlist.
const user1 = { firstName:'Anurag', lastName: 'Sharma', pata : { city: 'Delhi', pincode: 123456 }, subject: ['CS', 'DS', 'CN'] } const user2 = JSON.parse(JSON.stringify(user1)) This will make it Deep copy! Thankyou so much Sir.
Thank you so much, Sir, for sharing your excellent knowledge with us. Every day, I watch 5-6 of your JavaScript videos and practice regularly. A few days ago, I was looking for the best JavaScript tutorials on UA-cam and luckily found your channel. Once again, thank you so much. I hope one day I will become a good developer.
Sir thank you sir I'm learning JavaScript from this playlist.. And it is helping me a lot because I'm in internship and I'm learning js from here❤..thank you a lot sir...
Summary: Data types are based on memory allocation. 1) Stack Memory 2) Heap Memory 1) Stack Memory: Primitive datatype is call by Value. So in Stack Memory we get copy of the value that is why we can change it. 2) Heap Memory: Non-Primitive datatypes is call by reference. So in heap memory we get direct reference of the that value so we are changing the reference value. Note: non-primitive is object. also array. Deep copy Answer : const user2 = JSON.parse(JSON.stringify(user1)); Because a deep copy shares no references with its source object, any changes made to the deep copy do not affect the source object. new way: const user2 = structuredClone(user1); Because JSON.stringify() can not handle every type that we use in javascript. That is why this was introduced. correct me if i am wrong.
No, structuredClone() can not clone the function or methods if object contains methods. And also DOM Nodes and Symbol properties can not be cloned by this method.
@@nobody20022 I just chatgpt it and it says it can clone all kind of complex data including function. May be they updated the functionality of this method. But thanx for suggesting this method, it is really helpful rather than writing that json.parse and json.stringify syntax. There is one more method comes with lodash library called _.clonedeep()...It is works for all kind of data.
@@nobody20022 I just chatGpt it, and it says that this method can handle all kinda of complex data including function. This is may be because of new update to the method. But there is another library function called ._cloneDeep() come with lodash library which can also handle all kinds of complex data. Thanx for the info about structuredClone() btw.
We can deep copy any object or array using JSON.stringify() and JSON.parse() method ... First we will serialize the given nested object or array using JSON.stringify() method which will convert the whole object/array into a string and hence will give a totally different address to this string Then we will deserialize this string using JSON.parse() method which will convert the string back into object/array with completely new address for both the main object/array and also a new address for the nested object/array And hence any changes made to the nested object/array will not be reflected in the original one having different addresses/references
sir i just want to know how do you know things in such depth ,its really awesome .please tell me how we can learn anything in such depth ,means I know how it work but I want to know what it used behind the scene.
const numbers = [1, [2], [3, [4]], 5]; // Using JavaScript JSON.parse(JSON.stringify(numbers)); // Using Lodash _.cloneDeep(numbers); JSON.stringify/parse only work with Number and String and Object literal without function or Symbol properties. deepClone work with all types, function and Symbol are copied by reference.
That's correct, now there is a new built-in function in JavaScript called structuredClone() If you pass any object inside this function it will make a deep copy and return the new object. And this function is available globally.
@procodrr : The web API structuredClone() also creates deep copies and has the advantage of allowing transferable objects in the source to be transferred to the new copy, rather than just cloned. It also handles more data types, such as Error. But note that structuredClone() isn't a feature of the JavaScript language itself - instead it's a feature of browsers and other JavaScript hosts that implement web APIs. And calling structuredClone() to clone a non-serializable object will fail in the same way that calling JSON.stringify() to serialize it will fail.
Anurag bhaiya for performing Deep Copy if we have an User object created like you metioned in the video then we can perform deep copy as below const user3 = JSON.parse(JSON.stringify(user)); We pass the variable which we want to clone inside JSON.stringify method.
As it seen in the snapshot even the address of the immediate key-value pair such as firstName and lastName have the same address in both the objects but changing one of them is not reflecting in the other one(in Shallow copying). Can someone explain it ?
let user2 = JSON.parse(JSON.stringify(user1)) here stringify will convert it into a string and parse will again convert back to normal code so now this code is not associated with any memory address am I right?
using shallow copy it copy only the outer array or objects , if array or object contains nested array or object then it shared for both old and new array
Bhaiya array behind the scenes object kaise hai isko bataiye thoda? Object me to key value ki tarah store hota hai array me to aisa nahi hota to behind the scenes object kaise hua plz explain kariye isko?
Sir in case of string & number pass by value and objects and arrays passed by reference. Because objects are pointing to same address when one object changes another changes. In case of number it is pass by value not by reference
I have not explained how to do deep copy it in a separate video. I have explained that the concept that what is deep copy in this video itself. It is not you assignment to find out how can we do deep copy.
Sir, I can find it on internet (deep copy), But your explanation is wonderful. Hence, I am looking for your video about deep copy. Thankyou sir for your reply.
Deep copy can be achieved by using JSON.stingify and JSON.parse const user1={firstName:'Anurag',lastName:'Singh',pata:{city:'Delhi',pincode:'998988'},subject:['Physics','CS','Math']} const user2=JSON.parse(JSON.stringify(user1))//Creates Deep Copy
Amazing!! Everyone say that an object is referred to by a memory address but no one showed it. You are showing it and also how it is happening in browser.
Now this makes more sense and also easy to remember.
And @40.00 you showed how one object is stored in another object, that blew my mind, it was so simple.
Thanks for sharing the knowledge.
Awesome bro, I'm glad that you're learning so much.
Keep Learning and Sharing 😊
By using Json we can make deepcopy let's understand through an example:
const ingredientsList = ["noodles", { list: ["eggs", "flour", "water"] }];
const ingredientsListDeepCopy = JSON.parse(JSON.stringify(ingredientsList));
JSON.stringf() converts the original element of an array into strings and then JSON.parse converts the strings elements into Object / Array by doing this the address of original array will change and this time we'll get deep copy of previous object / Array not shallow copy.
may be I'm not able to make more clear to you. For more clarity you can see mdn.
Thankyou Anurag sir for this Wonderful playlist.
Bro You can use structuredClone() method for deep cloning or use the lodash function for deep cloning.
ye naye jamane ke codrr hai..... andar tak ghus kar samjhayenge.....
Awesome .....❣❣
const user1 = {
firstName:'Anurag',
lastName: 'Sharma',
pata : {
city: 'Delhi',
pincode: 123456
},
subject: ['CS', 'DS', 'CN']
}
const user2 = JSON.parse(JSON.stringify(user1))
This will make it Deep copy!
Thankyou so much Sir.
This channel is underrated 👏❤, Great information Sir!
Awesome, keep learning and sharing 😊
WOW sir, Shallow copy Bahut Ache se samaj Agya Muje😁🤩🙏
Thank you so much, Sir, for sharing your excellent knowledge with us. Every day, I watch 5-6 of your JavaScript videos and practice regularly. A few days ago, I was looking for the best JavaScript tutorials on UA-cam and luckily found your channel. Once again, thank you so much. I hope one day I will become a good developer.
Awesome, keep learning and growing 😊
Great information sir thank you.
Episode 23 completed 😊 excellent video sir, bohot kuch janne ko mila. Thank you so much ❤
Anurag Sir OP in the chat
Hat's off to you
Excellent teaching 💥💥💥
bht ache se explain kie hein. core concepts strong ho rhe hein mere. keep up the good work
Great video sir🙏
Learning a lot from you! Thanks! 🌟
Keep learning and growing! 😊
Seriously hats off to u sir for providing that deep level explanation with visuals..❤
Day 10: video 24 completed thank you sir making such great video😊
Thanks for this awesome lecture ☺️
Welcome Bro 😊
Sir thank you sir I'm learning JavaScript from this playlist.. And it is helping me a lot because I'm in internship and I'm learning js from here❤..thank you a lot sir...
27:38 Object.assign must be start with capital O.
Sir i am following your js course and saini sir grt work 🎉❤
Thank you for making this Series ❤
using 1) JSON.parse(JSON.stringify(variable name)) 2)structuredClone(variable name)
Great Video Sir🔥
ThankYou so much sir for sharing your deep knowledge.🥰🥰
Sir seriously you are amazing Thankyou for sharing and seriously if i would found your channel eair then my 50000 would be saved yaar
Koi baat nahi, now that you've found it, learn well. All the best 👍
sir thank you so much, i love you hogaya
DeepCopy
Const obj1 = {
fName: 'Arzoo',
lName:'Duddy',
moreInfo:{
City: 'Gurgaon',
Pincode: 123456,
},
Const newObj = JSON.parse(JSON.stringify(ojb1)) 50:05
let user2 = JSON.parse(JSON.stringify(user1))
sir e chapter ekdum mast he
Awesome, keep learning and sharing 😊
first option will be the 'for loop' and second option will be JSON.parse(JSON.stringify());
Ek number bhai
let originalArray = [{a: 1}, {b: 2}, {c: 3}];
let deepCopy = JSON.parse(JSON.stringify(originalArray));
// Modifying a nested object in the deep copy
deepCopy[0].a = 10;
console.log(originalArray); // Output: [{a: 1}, {b: 2}, {c: 3}]
console.log(deepCopy); // Output: [{a: 10}, {b: 2}, {c: 3}]
Very interesting video thanks
Sir aap kon sa extension use karte ho
const originalArray = [1, 2, { a: 3, b: { c: 4 } }];
const deepCopy = structuredClone(originalArray);
deepCopy[2].b.c = 10;
console.log(originalArray[2].b.c); // 4 (remains unchanged)
console.log(deepCopy[2].b.c); // 10 (changed in the deep copy)
Summary:
Data types are based on memory allocation.
1) Stack Memory 2) Heap Memory
1) Stack Memory: Primitive datatype is call by Value. So in Stack Memory we get copy of the value that is why we can change it.
2) Heap Memory: Non-Primitive datatypes is call by reference. So in heap memory we get direct reference of the that value so we are changing the reference value.
Note: non-primitive is object. also array.
Deep copy Answer : const user2 = JSON.parse(JSON.stringify(user1));
Because a deep copy shares no references with its source object, any changes made to the deep copy do not affect the source object.
new way: const user2 = structuredClone(user1);
Because JSON.stringify() can not handle every type that we use in javascript. That is why this was introduced.
correct me if i am wrong.
does strructuredClone() method clones object methods as well?
No, structuredClone() can not clone the function or methods if object contains methods.
And also DOM Nodes and Symbol properties can not be cloned by this method.
@@nobody20022 I just chatgpt it and it says it can clone all kind of complex data including function. May be they updated the functionality of this method. But thanx for suggesting this method, it is really helpful rather than writing that json.parse and json.stringify syntax.
There is one more method comes with lodash library called _.clonedeep()...It is works for all kind of data.
@@nobody20022 I just chatGpt it, and it says that this method can handle all kinda of complex data including function. This is may be because of new update to the method.
But there is another library function called ._cloneDeep() come with lodash library which can also handle all kinds of complex data.
Thanx for the info about structuredClone() btw.
We can deep copy any object or array using JSON.stringify() and JSON.parse() method ...
First we will serialize the given nested object or array using JSON.stringify() method which will convert the whole object/array into a string and hence will give a totally different address to this string
Then we will deserialize this string using JSON.parse() method which will convert the string back into object/array with completely new address for both the main object/array and also a new address for the nested object/array
And hence any changes made to the nested object/array will not be reflected in the original one having different addresses/references
amazing
Deep Copy
const user1DeepCopy = JSON.parse(JSON.stringify(user1))
sir i just want to know how do you know things in such depth ,its really awesome .please tell me how we can learn anything in such depth ,means I know how it work but I want to know what it used behind the scene.
Explore the DevTools you'll get to learn lots of new things.
Const user2 = JSON.parse(JSON.stringify(user1))
If we can assign an array to another array like objects using the assign() method can we use the same method for functions if yes what the use cases??
waiting for more interviews
dhanyvad
const numbers = [1, [2], [3, [4]], 5]; // Using JavaScript JSON.parse(JSON.stringify(numbers)); // Using Lodash _.cloneDeep(numbers);
JSON.stringify/parse only work with Number and String and Object literal without function or Symbol properties.
deepClone work with all types, function and Symbol are copied by reference.
That's correct, now there is a new built-in function in JavaScript called structuredClone()
If you pass any object inside this function it will make a deep copy and return the new object.
And this function is available globally.
@procodrr : The web API structuredClone() also creates deep copies and has the advantage of allowing transferable objects in the source to be transferred to the new copy, rather than just cloned. It also handles more data types, such as Error. But note that structuredClone() isn't a feature of the JavaScript language itself - instead it's a feature of browsers and other JavaScript hosts that implement web APIs. And calling structuredClone() to clone a non-serializable object will fail in the same way that calling JSON.stringify() to serialize it will fail.
great work brother. I have doubt how can I search strings or Find Strings in DEV tools ?
Ctrl + F
Hello sir. This is correct...)
const user2 = JSON.parse(JSON.stringify(user1));
Yes, that is correct.
Shallow Means, We Copy Values of an Object at Single Level Deep . ( Sir This Much Definition Okay For Interview ? )
Yes
Answer of Deep copy
const person = {
name : 'Don',
hoobies :[
"Web development",
"Cricket",
]
}
const perosonecloned = structuredClone(person)
thankyou guruji
Anurag bhaiya for performing Deep Copy
if we have an User object created like you metioned in the video then we can perform deep copy as below
const user3 = JSON.parse(JSON.stringify(user));
We pass the variable which we want to clone inside JSON.stringify method.
Correct
@@procodrr Thank you bhaiya 😄
As it seen in the snapshot even the address of the immediate key-value pair such as firstName and lastName have the same address in both the objects but changing one of them is not reflecting in the other one(in Shallow copying). Can someone explain it ?
let user2 = JSON.parse(JSON.stringify(user1))
here stringify will convert it into a string and parse will again convert back to normal code so now this code is not associated with any memory address am I right?
😘best way is (structuredClone(variableName)
const user1 = {};
let user2 = structuredClone(user1);😏
Amazing
using shallow copy it copy only the outer array or objects , if array or object contains nested array or object then it shared for both old and new array
const obj3 = { a: 0, b: { c: 0 } };
const obj4 = structuredClone(obj3);
obj3.a = 4;
obj3.b.c = 4;
console.log(obj4); // { a: 0, b: { c: 0 } }
Bhaiya array behind the scenes object kaise hai isko bataiye thoda? Object me to key value ki tarah store hota hai array me to aisa nahi hota to behind the scenes object kaise hua plz explain kariye isko?
Structuredclone(obj)
Sir in case of string & number pass by value and objects and arrays passed by reference. Because objects are pointing to same address when one object changes another changes. In case of number it is pass by value not by reference
the reference to an object is in itself a value,
Sir jo app ye search karte ho memory devtools me searchbaar kaise ata h humara nahi ata h jaise app ne anurag and mango search kiya
Ctrl + F press karo, aa jayega.
Bhai agar memory allocation ignore Kara Jaye toh aage Jake koi dikkat dega kya
Haa, memory leak ho sakta hai.
deepCopy = JSON.parse(JSON.stringify
in which video you explain deep copy concept?
I have not explained how to do deep copy it in a separate video.
I have explained that the concept that what is deep copy in this video itself.
It is not you assignment to find out how can we do deep copy.
Sir, I can find it on internet (deep copy), But your explanation is wonderful. Hence, I am looking for your video about deep copy. Thankyou sir for your reply.
const obj1 = {
name: "prem",
age: 20,
address: {
city: "jaipur",
pincode: 123,
},
};
const obj2 = { ...obj1 }; // shalow copy
const obj2 = { ...obj1, address: { ...obj1.address } }; // deep copy
is s obj1 k nested obj p koi effect nhi hua, is it a good example sir ??????
Correct 💯
Please sir learn recursion in js
Sir your info is very understanding... But in a video uh don't tell us how to search string in a snapshot . We cannot find option of search
Press Ctrl + F
Cont user1 = JSON.parse(JSON.stringify(user1);
sir unable to find array in system/context
ua-cam.com/video/Gqlv6inCZqI/v-deo.htmlsi=d0D6N-jwT6RLQkYq
Did you watch this video?
@@procodrryes now I'm able to find array objects strings in system/context thankyou very much sir ❤❤
Const newObj = JSON.parse(JSON.stringify(myObj)
object copy by reference || reference type value hota hai
nice
core video
mil gya ctrl +f
Awesome 👍
Sir, Git par bhi tutorial bana do 🙏🏼
JavaScript ke baad banayenge
@@procodrr sir git ka use kha pr h .
Apne bade codebase ko manage karne ke liye golit ka use karte hain
@@procodrr acha kitne din ka course h
Six months, lekin tum isko 4 months mein bhi khatam kar sakte ho jab sare lectures record ho jayenge
Deep Copy
import json
import copy
# Example JSON object
original_json = {
"name": "Alice",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Wonderland"
},
"hobbies": ["reading", "chess"]
}
# Deep copy using the copy module
deep_copied_json = copy.deepcopy(original_json)
# Modify the deep copied object
deep_copied_json['name'] = "Bob"
deep_copied_json['address']['city'] = "Neverland"
# Output the original and modified JSON objects
print("Original JSON:")
print(json.dumps(original_json, indent=4))
print("
Deep Copied JSON:")
print(json.dumps(deep_copied_json, indent=4))
For Deep Copying
const myFruits = JSON.parse(JSON.stringify(fruits))
const user2 = JSON.parse(JSON.stringify(user1))
Episode 23 completed bhaiya ji. Good night. Kal milte hain bhaiya. ❤
Good night ❤️
agar pahle pata chalta apke channel ka to mere 70000 bach jate
Koi baat nahi bro, jyada tension mat lo. Tum bas achhe se padhai karo job lagegi to sab waapas aa jayega.
Keep Learning and Sharing 😊
Sach me 70k waste kare tune JS pe😂
Joot mat bol bro Tera sakal sai lagta hai ki tum 1 poor admi hai admi hai 😂😂😂
JSON.parse(JSON.stringify(nestedobj));
I found this code on mdn
😍❤
const obj1 = {
firstName: 'Chaman',
lastName: 'Chutiya',
pata: {
city: 'Bangalore',
country: 'India',
pinCode: 121005,
},
}
// deep copy
const obj2 = JSON.parse(JSON.stringify(obj1))
this is deep copy synatx
Json.stringify karke
But when it comes to functions inside json.stringify fails there
Simple solution use lodash
Yes, correct 👍
Lodash ka use kar sakte hai ( Functions bhi kaam kartein hain uske sath )
..........
Pehle khud cheezko ko study kro aur logo ko tum toh khrab kr doge bhai deep copy kaha btaya don't see this video guys it's not correct
bhai kitni spoon fedding chaiye apko?
@@Vikas-sc4jd tumhe need hai
Bhai sir ek well experience developer Bangalore me job krte hai hai aur har developer kabhe n kabhe kuch chize bhul jaata he hai @@user-un3lj7iu7v
Deep copy can be achieved by using JSON.stingify and JSON.parse
const user1={firstName:'Anurag',lastName:'Singh',pata:{city:'Delhi',pincode:'998988'},subject:['Physics','CS','Math']}
const user2=JSON.parse(JSON.stringify(user1))//Creates Deep Copy
const names = {
firstName: "alka",
lastName: "Sharma",
pata: {
firstLine: "jssgh",
secondLine: "shsh",
pincode: 978766,
nearBuy: { naerone: "hdfhh", near2: "dbbfh" },
},
subject: ["chem", "CS", "Math"],
};
// Shallow COPY (SPREAD OPERATOR {...})
// const names2 = { ...names };
// DEEP COPY (JSON METHOD)
const names2 = JSON.parse(JSON.stringify(names));
names2.lastName = "verma";
names2.pata.pincode = 12345;
names2.subject.push("Bio");
names2.pata.nearBuy.near2 = "qwerty";
console.log(names2);
console.log(names);
const user1 = {
firstname : 'Hukma',
lastname : 'Choudhary',
pata : {
city : "Jaipur",
pincode : 302012
},
subjects : ['maths','chemistry','physics','js', 'cs']
}
// const user2 = {...user1} //Shallow Copy
// Deep copy
const user2 = JSON.parse(JSON.stringify(user1));
Sir @procodrr,
What about this?
const user2 = structuredClone(user1)
// Deep copy
const employee = {
eid: "E102",
ename: "Jack",
eaddress: "New York",
salary: 50000
}
console.log("=========Deep Copy========");
const newEmployee = JSON.parse(JSON.stringify(employee));
console.log("Employee=> ", employee);
console.log("New Employee=> ", newEmployee);
DeepCopy
Const obj1 = {
fName: 'Arzoo',
lName:'Duddy',
moreInfo:{
City: 'Gurgaon',
Pincode: 123456,
},
Const newObj = JSON.parse(JSON.stringify(ojb1)) 50:05