deep clone Object js code example
Example 1: deep clone object javascript
JSON.parse(JSON.stringify(object))
Example 2: clone javascript object
let clone = Object.assign({}, objToClone);
Example 3: how to make a deep copy in javascript
JSON.parse(JSON.stringify(o))
Example 4: deep clone javascript object
const deepCopyFunction = (inObject) => {
let outObject, value, key
if (typeof inObject !== "object" || inObject === null) {
return inObject
}
outObject = Array.isArray(inObject) ? [] : {}
for (key in inObject) {
value = inObject[key]
outObject[key] = deepCopyFunction(value)
}
return outObject
}