Nodejs: how to clone an object

后端 未结 12 2070
情书的邮戳
情书的邮戳 2020-12-04 18:32

If I clone an array, I use cloneArr = arr.slice()

I want to know how to clone an object in nodejs.

12条回答
  •  青春惊慌失措
    2020-12-04 19:26

    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!

提交回复
热议问题