How to Deep clone in javascript

前端 未结 19 1529
-上瘾入骨i
-上瘾入骨i 2020-11-22 02:06

How do you deep clone a JavaScript object?

I know there are various functions based on frameworks like JSON.parse(JSON.stringify(o)) and $.extend(t

19条回答
  •  清酒与你
    2020-11-22 03:00

    Lo-Dash, now a superset of Underscore.js, has a couple of deep clone functions:

    • _.cloneDeep(object)

    • _.cloneDeepWith(object, (val) => {if(_.isElement(val)) return val.cloneNode(true)})

      the second parameter is a function that is invoked to produce the cloned value.

    From an answer of the author himself:

    lodash underscore build is provided to ensure compatibility with the latest stable version of Underscore.

提交回复
热议问题