Skip to content

Instantly share code, notes, and snippets.

@ldong
Created February 22, 2017 09:35
Show Gist options
  • Save ldong/ff853ef85ea11f7843a6a0241114031f to your computer and use it in GitHub Desktop.
Save ldong/ff853ef85ea11f7843a6a0241114031f to your computer and use it in GitHub Desktop.
deep clone
function cloneObject(obj) {
  const clone = Object.keys(obj).reduce((acc, prop) => {
  	acc[prop] = obj[prop] === Object(obj[prop]) ? cloneObject(obj[prop]) : obj[prop];
    return acc;
  }, {});

  return clone;
};

Based on Lyndsey Browning's Deep Cloning JavaScript Objects that added

  1. Object check Check if a value is an object in JavaScript
  2. Use Recursion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment