js deep object and array clone custom function code example
Example 1: 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
}
Example 2: js deep copy array
const numbers = [1, [2], [3, [4]], 5];
JSON.parse(JSON.stringify(numbers));
_.cloneDeep(numbers);