Skip to content

Instantly share code, notes, and snippets.

@CodinCat
Created August 15, 2016 08:31
Show Gist options
  • Select an option

  • Save CodinCat/d2bdc4b721efc8bba01c8c2662b2e15b to your computer and use it in GitHub Desktop.

Select an option

Save CodinCat/d2bdc4b721efc8bba01c8c2662b2e15b to your computer and use it in GitHub Desktop.

Revisions

  1. CodinCat created this gist Aug 15, 2016.
    18 changes: 18 additions & 0 deletions deep-clone.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    function deepClone(obj) {
    if (Array.isArray(obj))
    return obj.map((el) => deepClone(el))

    if (
    typeof obj !== 'object' ||
    Object.prototype.toString.call(obj) !== '[object Object]' ||
    obj === null
    )
    return obj

    let clone = {}
    for (let i in obj)
    if (obj.hasOwnProperty(i))
    clone[i] = deepClone(obj[i])

    return clone
    }