# Duplicate

Duplicates an object - either shallow or deep

## Method Signature

```
Duplicate(object=[any], deep=[boolean])
```

### Arguments

| Argument | Type      | Required | Description                                                                                       | Default |
| -------- | --------- | -------- | ------------------------------------------------------------------------------------------------- | ------- |
| `object` | `any`     | `true`   | Any object to duplicate                                                                           |         |
| `deep`   | `boolean` | `false`  | Whether to deep copy the object or make a shallow copy (e.g. only the top level keys in a struct) | `true`  |

## Examples

### Changing a struct compared to changing its copy

`myNewStruct` holds a reference to `myStruct` so if you change `myNewStruct`, `myStruct` is changed accordingly as well, because they are the same struct just assigned to two variables. In comparison `myOtherNewStruct` is a copy so if you change `myOtherNewStruct`, `myStruct` stays untouched because with the duplicate, a new, unique structure with the same key-value pairs is created thus they do not share the same reference

[Run Example](https://try.boxlang.io/?code=eJzLrQwuKSpNLlGwVahW4OJUSsvPV1KwUlDyyS9KzVXILCguzVXSAYonJRaBxZMSq5S4aq25civ9UsvhWnOhpoDE%2FUsyUouQJVNKC3IykxNLUjXg6hQ0UUzQc%2FP3BypUSszITS1RKC7NU8I0CKYmJT8nv0ihOLMEqKa8KLMk1b%2B0pKC0BGE2WKGagpLCo7ZJQFJNAd0iVDkstgAdBwDc31vf)

```java
myStruct = { 
	"foo" : "Lorem ipsum",
	"bar" : "baz"
};
myNewStruct = myStruct;
myOtherNewStruct = duplicate( myStruct );
myNewStruct.FOO = "ahmet sun";
myOtherNewStruct.FOO = "dolor sit";
writeOutput( myStruct.FOO & " → " & myNewStruct.FOO & " → " & myOtherNewStruct.FOO );

```

Result: ahmet sun → ahmet sun → dolor sit

### Additional Examples

[Run Example](https://try.boxlang.io/?code=eJwrSC0qzs9TsFWoVuDidPMMCg5RsFJQckpMSlXS4eL0cYTwg0pLMpS4aq25UkpzCzQUCiCaNK0V9PUVQGoVQAq4knPy81KBRqWUFuRkJieWpCKphOqEKEHXCFGlB7bNVkEpODcTaB1uy8DyaAZyoZoIAKLOP%2BU%3D)

```java
person = { 
	FIRST : "Babe",
	LAST : "Ruth"
};
dump( person ); // Babe Ruth
clone = duplicate( person );
dump( clone ); // Babe Ruth
person.LAST = "Smith";
dump( person ); // Babe Smith
dump( clone );
 // Babe Ruth

```

## Related

* [ApplicationRestart](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/applicationrestart)
* [ApplicationStartTime](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/applicationstarttime)
* [ApplicationStop](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/applicationstop)
* [BoxAnnounce](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/boxannounce)
* [BoxAnnounceAsync](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/boxannounceasync)
* [BoxAST](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/boxast)
* [BoxModuleReload](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/boxmodulereload)
* [BoxRegisterInterceptionPoints](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/boxregisterinterceptionpoints)
* [BoxRegisterInterceptor](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/boxregisterinterceptor)
* [BoxRegisterRequestInterceptor](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/boxregisterrequestinterceptor)
* [BoxUnregisterInterceptor](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/boxunregisterinterceptor)
* [BoxUnregisterRequestInterceptor](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/boxunregisterrequestinterceptor)
* [CallStackGet](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/callstackget)
* [CreateGUID](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/createguid)
* [CreateObject](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/createobject)
* [CreateUUID](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/createuuid)
* [DE](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/de)
* [DebugBoxContexts](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/debugboxcontexts)
* [Dump](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/dump)
* [echo](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/echo)
* [EncodeForHTML](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/encodeforhtml)
* [GetApplicationMetadata](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getapplicationmetadata)
* [GetBaseTagData](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getbasetagdata)
* [GetBaseTagList](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getbasetaglist)
* [GetBaseTemplatePath](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getbasetemplatepath)
* [GetBoxContext](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getboxcontext)
* [GetBoxRuntime](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getboxruntime)
* [GetBoxVersionInfo](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getboxversioninfo)
* [GetClassMetadata](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getclassmetadata)
* [GetComponentList](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getcomponentlist)
* [GetContextRoot](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getcontextroot)
* [GetCurrentTemplatePath](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getcurrenttemplatepath)
* [GetFileFromPath](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getfilefrompath)
* [GetFunctionCalledName](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getfunctioncalledname)
* [GetFunctionList](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getfunctionlist)
* [GetModuleInfo](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getmoduleinfo)
* [GetModuleList](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getmodulelist)
* [GetRequestClassLoader](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getrequestclassloader)
* [GetSemver](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getsemver)
* [GetSystemSetting](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/getsystemsetting)
* [GetTempDirectory](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/gettempdirectory)
* [GetTickCount](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/gettickcount)
* [htmlEditFormat](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/htmleditformat)
* [IIF](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/iif)
* [Invoke](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/invoke)
* [IsInstanceOf](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/isinstanceof)
* [JavaCast](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/javacast)
* [ObjectDeserialize](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/objectdeserialize)
* [ObjectSerialize](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/objectserialize)
* [PagePoolClear](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/pagepoolclear)
* [Print](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/print)
* [Println](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/println)
* [RunThreadInContext](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/runthreadincontext)
* [SessionInvalidate](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/sessioninvalidate)
* [SessionRotate](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/sessionrotate)
* [SessionStartTime](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/sessionstarttime)
* [Sleep](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/sleep)
* [SystemCacheClear](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/systemcacheclear)
* [SystemExecute](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/systemexecute)
* [SystemOutput](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/systemoutput)
* [Throw](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/throw)
* [Trace](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/trace)
* [URLDecode](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/urldecode)
* [URLEncodedFormat](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/urlencodedformat)
* [writeDump](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/writedump)
* [WriteLog](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/writelog)
* [WriteOutput](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/system/writeoutput)
