Important Javascript functions you have to know to be a better developer Sam Sonter on March 05, 2022 Hi everybody, today I am going to show examples of few important Javascript functions. DEEP COPY JavaScript allows you to deep copy an ... Read full post Submit Preview Dismiss Collapse Expand Larson Larson Larson Follow Location Germany Work Freelancer Joined Aug 9, 2018 • Mar 5 '22 • Edited on Mar 5 • Edited Dropdown menu Copy link Hide You should not use JSON.stringify to clone object. It leads to problems because it can't strinigfy functions,symbol etc. JSON.stringify({ fun: () => console.log('lol'), s: Symbol(), u: undefined, n: NaN, in: Infinity }) '{"n":null,"in":null}' Enter fullscreen mode Exit fullscreen mode Just use lodash. Or write your own deep cloning algo. Or when using the browser you can use structuredClone (there are some limits too). Collapse Expand peerreynders peerreynders peerreynders Follow Joined Feb 29, 2020 • Mar 5 '22 Dropdown menu Copy link Hide Node.js 17.0.0 added structuredClone(value[, options]) Before that use v8.serialize(value) and v8.deserialize(buffer). Then there is flatted which adds recursive references and RecursiveMap on top of JSON (still no functions, symbols, etc.). Collapse Expand Sam Sonter Sam Sonter Sam Sonter Follow Just normal guy that is into programming Email samuel.aarga@gmail.com Location Shenzhen, China Education BSc Computer Science and Tech Work SYCRA TECH Joined Jun 30, 2021 • Mar 5 '22 Dropdown menu Copy link Hide noted. thanks Collapse Expand Sam Sonter Sam Sonter Sam Sonter Follow Just normal guy that is into programming Email samuel.aarga@gmail.com Location Shenzhen, China Education BSc Computer Science and Tech Work SYCRA TECH Joined Jun 30, 2021 • Mar 5 '22 Dropdown menu Copy link Hide noted. thanks Collapse Expand Jackson Kasi Jackson Kasi Jackson Kasi Follow Self-taught tech enthusiast with a passion for continuous learning and innovative solutions Email nammalvar888@gmail.com Location India Education Completed School, no College but Learner until Death 😎 Work Just survive Joined Dec 25, 2020 • Mar 5 '22 Dropdown menu Copy link Hide wow, thanks, it's really helpful for me :) Collapse Expand Tamir Tamir Tamir Follow Joined May 21, 2021 • Mar 6 '22 Dropdown menu Copy link Hide "And you can google any of these if needed." reading relaxed me instantly. Collapse Expand codingjlu codingjlu codingjlu Follow Joined Jun 15, 2021 • Mar 7 '22 Dropdown menu Copy link Hide It's also important to remember that JSON.stringify cannot copy functions, so the "deep copy" only works for regular data types and objects. Collapse Expand CuriousDev CuriousDev CuriousDev Follow Location Germany Joined Jan 8, 2022 • Mar 5 '22 Dropdown menu Copy link Hide Thank you and I would like to add, that creating a clone with JSON object (with parse and stringify) has some limitations! It will not work with every object. Collapse Expand Sam Sonter Sam Sonter Sam Sonter Follow Just normal guy that is into programming Email samuel.aarga@gmail.com Location Shenzhen, China Education BSc Computer Science and Tech Work SYCRA TECH Joined Jun 30, 2021 • Mar 6 '22 Dropdown menu Copy link Hide hhahahhahah. what work, works Collapse Expand Mateusz Skrobiś Mateusz Skrobiś Mateusz Skrobiś Follow Work Frontend Developer Joined Feb 5, 2022 • Mar 11 '22 Dropdown menu Copy link Hide love that reply, thank u. Collapse Expand Sara (Risclover) Sara (Risclover) Sara (Risclover) Follow Joined Mar 7, 2022 • Mar 11 '22 Dropdown menu Copy link Hide These seem useful. Thanks for posting! Collapse Expand Sam Sonter Sam Sonter Sam Sonter Follow Just normal guy that is into programming Email samuel.aarga@gmail.com Location Shenzhen, China Education BSc Computer Science and Tech Work SYCRA TECH Joined Jun 30, 2021 • Mar 12 '22 Dropdown menu Copy link Hide you are welcome Sanzhar Danybayev Sanzhar Danybayev Sanzhar Danybayev Follow Joined Jan 17, 2018 • Mar 20 '22 Dropdown menu Copy link Hide That's good too hear. I like your attitude. No offense. Might have misread your comment. Collapse Expand Sanzhar Danybayev Sanzhar Danybayev Sanzhar Danybayev Follow Joined Jan 17, 2018 • Mar 20 '22 Dropdown menu Copy link Hide Bro, you're a bit harsh. Though I would agree that title looks like a clickbait. Collapse Expand Sam Sonter Sam Sonter Sam Sonter Follow Just normal guy that is into programming Email samuel.aarga@gmail.com Location Shenzhen, China Education BSc Computer Science and Tech Work SYCRA TECH Joined Jun 30, 2021 • Mar 21 '22 Dropdown menu Copy link Hide I dont mind criticism. Its ok Sanzhar Danybayev Sanzhar Danybayev Sanzhar Danybayev Follow Joined Jan 17, 2018 • Mar 22 '22 Dropdown menu Copy link Hide You're opened minded. Much respected! Collapse Expand Long Cui Long Cui Long Cui Follow Joined Dec 17, 2020 • Mar 13 '22 Dropdown menu Copy link Hide The "Wait" function is wrong. Please see @luke shiru's comment. Collapse Expand Hélio oliveira Hélio oliveira Hélio oliveira Follow Location São Luís, MA, Brazil Joined Feb 28, 2018 • Apr 11 '22 Dropdown menu Copy link Hide There is some typos. Like DECTECT , Andriod, prama, visiabilty. Code of Conduct • Report abuse
You should not use JSON.stringify to clone object. It leads to problems because it can't strinigfy functions,symbol etc.
Just use lodash. Or write your own deep cloning algo. Or when using the browser you can use structuredClone (there are some limits too).
Node.js 17.0.0 added structuredClone(value[, options])
Before that use v8.serialize(value) and v8.deserialize(buffer).
Then there is flatted which adds recursive references and
RecursiveMapon top of JSON (still no functions, symbols, etc.).noted. thanks
noted. thanks
wow, thanks, it's really helpful for me :)
"And you can google any of these if needed." reading relaxed me instantly.
It's also important to remember that
JSON.stringifycannot copy functions, so the "deep copy" only works for regular data types and objects.Thank you and I would like to add, that creating a clone with JSON object (with parse and stringify) has some limitations! It will not work with every object.
hhahahhahah. what work, works
love that reply, thank u.
These seem useful. Thanks for posting!
you are welcome
That's good too hear. I like your attitude. No offense. Might have misread your comment.
Bro, you're a bit harsh. Though I would agree that title looks like a clickbait.
I dont mind criticism. Its ok
You're opened minded. Much respected!
The "Wait" function is wrong. Please see @luke shiru's comment.
There is some typos. Like DECTECT , Andriod, prama, visiabilty.