KoObO
0
Q:

deep clone object javascript

JSON.parse(JSON.stringify(object))
4
const deepCopyFunction = (inObject) => {
  let outObject, value, key

  if (typeof inObject !== "object" || inObject === null) {
    return inObject // Return the value if inObject is not an object
  }

  // Create an array or object to hold the values
  outObject = Array.isArray(inObject) ? [] : {}

  for (key in inObject) {
    value = inObject[key]

    // Recursively (deep) copy for nested objects, including arrays
    outObject[key] = deepCopyFunction(value)
  }

  return outObject
}
1
let a = { x: 10, y: 15 };
let b = Object.assign({}, a);
console.log(a);
//output: { x: 10, y: 15 }
console.log(b);
//output: { x: 10, y: 15 }
console.log(a === b);
//output: false
2
JSON.parse(JSON.stringify(o))
0

New to Communities?

Join the community