Deep Copying vs Shallow Copying

Deep Copying vs Shallow Copying

23,531 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

F F
F F - 21.06.2023 06:53

Not sure if I missed anything, but I dont think lodash deepclone is mentioned here. Very easy way to make deep clone.

Ответить
Jal Toorey
Jal Toorey - 06.03.2023 07:18

Awesome. I feel like I appreciate this as much as you would have hoped one does. ty.

Ответить
Máté Őri
Máté Őri - 03.12.2022 15:37

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.

Ответить
mz92
mz92 - 16.09.2022 16:25

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;
}

Ответить
Sam
Sam - 25.02.2022 23:25

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.

Ответить
Kumar Gourav
Kumar Gourav - 10.10.2021 09:00

Great Stuff and great teaching ... Thanks

Ответить
Chester XP
Chester XP - 25.09.2021 12:52

Another very cool tutorial!

Ответить
claudio andrade
claudio andrade - 09.09.2021 06:40

whats the difference between shallow and deep copy and assign by reference or value?

Ответить
Sphiwe Modise
Sphiwe Modise - 29.07.2021 00:48

Well explained. Thank you 😊
I was struggling with this the whole day.

Ответить
generationwolves
generationwolves - 14.03.2021 08:20

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.

Ответить
priyanka Dey
priyanka Dey - 02.03.2021 18:40

Thanks for the great examples! Awesome

Ответить
Rici Shrimple
Rici Shrimple - 27.02.2021 21:32

i lost the whole day because of this problem, thanks a lot that you showed me how to solve it

Ответить
Diwakar
Diwakar - 15.01.2021 20:07

A lot of great info in one video! thanks a lot!!
It would have been nicer to see that custom recursive function as well :)

Ответить
Prakarsh Shrivastava
Prakarsh Shrivastava - 14.12.2020 19:12

thank it was exactly what i was looking for

Ответить
vazhaabdu
vazhaabdu - 11.12.2020 15:29

thank you sir

Ответить
allezzthepunk
allezzthepunk - 28.11.2020 22:05

Does the immutable.js package make it easier to deal with these kinds of issues and copy methods also?

Ответить
Edsiaren Ignacio
Edsiaren Ignacio - 15.11.2020 22:02

Really good job explaining these. Thanks!

Ответить
Priyanka Mahour
Priyanka Mahour - 27.10.2020 17:21

Thanks a million, Great explanation. you cleared all my doubts.

Ответить
Shubham Ghosh
Shubham Ghosh - 21.07.2020 20:55

I was super excited to see you using 'Calcutta' as an example of a place. I live in Calcutta (currently known as Kolkata) 😀

Ответить
Hou-Ya Wang
Hou-Ya Wang - 30.06.2020 19:09

Great tutorial as always. Thank you very much Steve! : )

Ответить
rot rose
rot rose - 17.05.2020 15:06

Thank you for this clear explanation about shallow and deep copy. Only you can make this difficult concept accessible to everyone.

Ответить
nosuger bei
nosuger bei - 29.02.2020 05:38

You are the best!

Ответить
moksh dave
moksh dave - 16.01.2020 23:23

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 !

Ответить
Rits K
Rits K - 05.12.2019 22:24

Awesome video... You have explained very clearly. thankyou

Ответить
Олег Олейник
Олег Олейник - 26.08.2019 21:04

Thank you! Its really clear explanation :)

Ответить
Ahmed Shahrour
Ahmed Shahrour - 01.06.2019 20:59

Very simple and to the point! Thank you sir!

Ответить
Bidenoolo
Bidenoolo - 27.04.2019 19:04

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?

Ответить
Martin Bozinovski
Martin Bozinovski - 09.04.2019 08:49

I tried [ ] instead of { } for the Object.assign( [ ], (source). I don't think that is a shallow copy. Or am i just talking nonsense?

Ответить
Sanjay
Sanjay - 22.02.2019 16:57

We can use jquery extend method for deep copy

Ответить
Tushar Borawake
Tushar Borawake - 20.02.2019 15:28

How spread operator, array.concat(), array.from & array.slice become shallow copy ? it won't change original array/object.can you please elaborate.

Ответить
linuxsport
linuxsport - 04.02.2019 00:30

Thanks. It finally clicked.

Ответить
Cameron Chardukian
Cameron Chardukian - 02.02.2019 16:02

This is currently a tough concept for me to grasp. I'll have to come back and watch this video again.

Ответить
Praneet Singh
Praneet Singh - 20.01.2019 19:07

awesome...

Ответить
Martin 007
Martin 007 - 13.01.2019 23:24

Bravo!

Ответить
Learn Web
Learn Web - 11.10.2018 12:04

Best explanation of deep / shallow copy. Many thanks Steve.

Ответить
Federico Dubuch
Federico Dubuch - 29.08.2018 17:13

Very good! thanks :D

Ответить
LawZist
LawZist - 25.07.2018 01:34

great as always

Ответить