Skip to content

Latest commit

 

History

History
35 lines (27 loc) · 599 Bytes

File metadata and controls

35 lines (27 loc) · 599 Bytes

deepClone()

Overview

Creates a deep copy of an object, including nested objects and arrays.

Code

A screenshot of the titular code snippet

const deepClone = (obj) => {
  if (typeof obj !== "object" || obj === null) {
    return obj;
  }

  let copy;

  if (Array.isArray(obj)) {
    copy = [];
    for (let i = 0; i < obj.length; i++) {
      copy[i] = deepCopy(obj[i]);
    }
  } else {
    copy = {};
    for (let key in obj) {
      if (obj.hasOwnProperty(key)) {
        copy[key] = deepCopy(obj[key]);
      }
    }
  }

  return copy;
}