• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Deep clone a value in Node.js

Deep clone a value in Node.js

Deep clone a value in Node.js

If you want to deep clone a value in Node.js, you no longer need to use a library or the JSON.parse(JSON.stringify(value)) hack. You can use the new global function structuredClone()


const user = {
  name: "JS Snippets",
  address: { street: "Original Road", city: "Placeshire" },
};

const clonedUser = structuredClone(user);

clonedUser.address.street = "New Road";

console.log("user.address.street:", user.address.street);
// > Original Road

console.log("clonedUser.address.street:", clonedUser.address.street);
// > New Road

Source

https://github.com/roeib/JavaScript-snippets#structuredclone

Miscellaneous

Related Snippets:

  • Check if all elements in an array are equal
  • Drag and drop element in a list
  • Capitalize the first letter of a string
  • Check if the provided value is of the specified type

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us