Cloning an Object in Node.js

后端 未结 21 1963
情书的邮戳
情书的邮戳 2020-11-28 02:03

What is the best way to clone an object in node.js

e.g. I want to avoid the situation where:

var obj1 = {x: 5, y:5};
var obj2 = obj1;
obj2.x = 6;
con         


        
21条回答
  •  无人及你
    2020-11-28 02:41

    There are some Node modules out there if don't want to "roll your own". This one looks good: https://www.npmjs.com/package/clone

    Looks like it handles all kinds of stuff, including circular references. From the github page:

    clone masters cloning objects, arrays, Date objects, and RegEx objects. Everything is cloned recursively, so that you can clone dates in arrays in objects, for example. [...] Circular references? Yep!

提交回复
热议问题