Nodejs: how to clone an object

后端 未结 12 2087
情书的邮戳
情书的邮戳 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:33

    Object.assign hasn't been mentioned in any of above answers.

    let cloned = Object.assign({}, source);
    

    If you're on ES6 you can use the spread operator:

    let cloned = { ... source };
    

    Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

提交回复
热议问题