Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including deep-clone with all npm packages installed. Try it out:

var deepClone = require("deep-clone")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

deep-clone v3.0.3

Deep cloning of Arrays and plain Objects.

deep-clone

Deep cloning of Arrays and plain Objects.

Build Status Coverage Status

$ npm i -S deep-clone

deepClone(obj, [stringFormatter])

Recursively clone nested objects and arrays containing primitive data or nested objects and arrays containing primitive data. Clones Date objects too...

import deepClone from 'deep-clone'

const foo = { bar: 'baz' }
const fooClone = deepClone(foo)

assert.deepEqual(foo, fooClone)
assert.notEqual(foo, fooClone)

const arr = [{ foo: 'bar'}, { baz: 'qux'}]
const arrClone = deepClone(arr)

assert.deepEqual(arr, arrClone)
assert.notEqual(arr, arrClone)

Deep clone an Object or Array and format the keys.

import camelCase from 'camelcase'
import deepClone from 'deep-clone'

const foo = { bar_baz: 'qux' }
const fooClone = deepClone(foo, camelCase)

assert.deepEqual(fooClone, { barBaz: 'qux' })

Or...

import camelCase from 'camelcase'
import { formatKeys } from 'deep-clone'

const camelKeys = formatKeys(camelCase)
const arr = [{ foo_bar: 'baz' }, { qux_quux: 'corge' }]
const camelClone = camelKeys(arr)

assert.deepEqual(camelClone, [{ fooBar: 'baz' }, { quxQuux: 'corge' }])

Version 2 handles circular references using a Map.

const foo = { bar: 'baz' }

foo.qux = [foo]

const clone = deepClone(foo)

assert.deepEqual(clone, foo)
assert.equal(clone.qux[0], clone)

Other options:

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free