How avoid inner nested object mutations in js? [DEEP COPY] | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 6

How avoid inner nested object mutations in js? [DEEP COPY]

As no SQL database are is cool, I want to store document which comes from firebase to redux as a state, in my case document(data) is many objects which is nested inside each other. [Question]: objects are reference value type in Javascript, how could I prevents from object and its nested objects from mutations? There some solutions: 1. freeze method applied for the outer object but doesn't effect the inner nested objects. 2. spreed operator creats a shallow copy, but you have to spreed each nested obeject to prevents mutation, its tasks alots of time and make my code ugly and hard to handle. 3. There are some libraries which makes my app heavy. 🐧🤖 [Conclusion]: is there any pure Javascript pattern or algorithm to handle the original object and its nested objects mutations and we could use from its copy over and over again. https://code.sololearn.com/cMTf3r8asSm3/?ref=app

25th Apr 2023, 7:30 AM
Yasin Rahnaward
Yasin Rahnaward - avatar
11 ответов
+ 6
Yasin Rahnaward unfortunately Sololearn node version is only 14.14.0. open nodejs codebit, type console.log(process.version) if you are using node version 17 and above, you can use structuredClone
25th Apr 2023, 1:51 PM
Bob_Li
Bob_Li - avatar
+ 3
THANKS alot Bob_Li & ODLNT it's a smart solution we can creat a deep copy function and go deep throw our objects as we need and shallowcopy all our nested object to a new one. it is reusable and clean code. https://code.sololearn.com/cDCPR82q4sXh/?ref=app
25th Apr 2023, 12:46 PM
Yasin Rahnaward
Yasin Rahnaward - avatar
+ 2
Yasin Rahnaward unfortunately Sololearn node version is only 14.14.0. open nodejs codebit, type console.log(process.version) if you are using node version 17 and above, you can use structuredClone Bob_Li structuredClone is modern and best way which I was not aware , thank you 🤗🐧
25th Apr 2023, 1:50 PM
Yasin Rahnaward
Yasin Rahnaward - avatar
25th Apr 2023, 2:08 PM
Bob_Li
Bob_Li - avatar
+ 1
Yasin Rahnaward Oh, I guess I misunderstood your question. You don't want a frozen object, but a copy of the object that you can modify without affecting the original object. I thought you wanted a completely frozen object.
25th Apr 2023, 1:01 PM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li Exactly I just want to break the reference between original and the copy one🐧😊
25th Apr 2023, 1:04 PM
Yasin Rahnaward
Yasin Rahnaward - avatar
0
Bob_Li THANKS With a deep freeze we can not copy the object with a different value and it's not make a sense, we should still copy the object with different value but the base[original] object values must not change.
25th Apr 2023, 11:03 AM
Yasin Rahnaward
Yasin Rahnaward - avatar
0
Yasin Rahnaward a deep copy. well, now you know how to do both 😁.
25th Apr 2023, 1:05 PM
Bob_Li
Bob_Li - avatar
0
سلام
26th Apr 2023, 3:52 PM
you
you - avatar
0
helo
26th Apr 2023, 3:53 PM
you
you - avatar