Комментарии:
Not sure if I missed anything, but I dont think lodash deepclone is mentioned here. Very easy way to make deep clone.
ОтветитьAwesome. I feel like I appreciate this as much as you would have hoped one does. ty.
ОтветитьSo many thanks for this explaining. After 2 days of trying to get work my code I founded your video. Now I know what is my problem.
ОтветитьI was working on a custom function that deepcopies objects (functions, and arrays aswell) and every property/element down the tree recursively;
So far, it seems to work in all scenarios, but maybe I am missing something:
function deepCopy(object) {
let objCopy;
if (Array.isArray(object)) {
objCopy = [...object];
for (let i = 0; i < object.length; i++) {
if (typeof object[i] === 'object') {
objCopy[i] = deepCopy(object[i]);
}
}
} else if (typeof object === 'function'){
objCopy = object.bind();
} else if (typeof object === 'object'){
objCopy = {...object};
const keys = Object.keys(object);
for (let i = 0; i < keys.length; i++) {
if (typeof object[keys[i]] === 'object') {
objCopy[keys[i]] = deepCopy(object[keys[i]]);
}
}
} else {
objCopy = object;
}
return objCopy;
}
Almost no one is explaining this topic this way.
About everyone else is making it more complex then it actually is.
Thank you so much.
Great Stuff and great teaching ... Thanks
ОтветитьAnother very cool tutorial!
Ответитьwhats the difference between shallow and deep copy and assign by reference or value?
ОтветитьWell explained. Thank you 😊
I was struggling with this the whole day.
Some additional info:
1. Be cautious while using the JSON method for deep copy. Objects like functions, dates, etc will be dropped, as JSON does not support these data types.
2. If you want a third-party solution for deep copy, look no further than lodash's cloneDeep() method. This method preserves all data types, and is the easiest method IMO.
Thanks for the great examples! Awesome
Ответитьi lost the whole day because of this problem, thanks a lot that you showed me how to solve it
ОтветитьA lot of great info in one video! thanks a lot!!
It would have been nicer to see that custom recursive function as well :)
thank it was exactly what i was looking for
Ответитьthank you sir
ОтветитьDoes the immutable.js package make it easier to deal with these kinds of issues and copy methods also?
ОтветитьReally good job explaining these. Thanks!
ОтветитьThanks a million, Great explanation. you cleared all my doubts.
ОтветитьI was super excited to see you using 'Calcutta' as an example of a place. I live in Calcutta (currently known as Kolkata) 😀
ОтветитьGreat tutorial as always. Thank you very much Steve! : )
ОтветитьThank you for this clear explanation about shallow and deep copy. Only you can make this difficult concept accessible to everyone.
ОтветитьYou are the best!
ОтветитьThank you soo much. This video actually helped alot .... You cleared so many doubts that other videos on shallow and deep copying were'nt able to do. Thanks again. Cheers !
ОтветитьAwesome video... You have explained very clearly. thankyou
ОтветитьThank you! Its really clear explanation :)
ОтветитьVery simple and to the point! Thank you sir!
ОтветитьWhen we deep copy objects using JSON.stringify and JSON.parse we lose our methods if we had, So what is right way of copying objects without losing methods or stuffs like that?
ОтветитьI tried [ ] instead of { } for the Object.assign( [ ], (source). I don't think that is a shallow copy. Or am i just talking nonsense?
ОтветитьWe can use jquery extend method for deep copy
ОтветитьHow spread operator, array.concat(), array.from & array.slice become shallow copy ? it won't change original array/object.can you please elaborate.
ОтветитьThanks. It finally clicked.
ОтветитьThis is currently a tough concept for me to grasp. I'll have to come back and watch this video again.
Ответитьawesome...
ОтветитьBravo!
ОтветитьBest explanation of deep / shallow copy. Many thanks Steve.
ОтветитьVery good! thanks :D
Ответитьgreat as always
Ответить