DEV Community 👩‍💻👨‍💻

Discussion on: The pitfalls 🕳️ of cloning objects in JS (with Object.assign & object spread)

patarapolw profile image
Pacharapol Withayasakpunt

yaml.load(yaml.dump(obj, options)), so that I can pass in options like no-refs.

I also use this to hash objects (options.sortKeys + SparkMD5)

Another options is to use lodash.cloneDeep

This will preserve special objects, such as RegExp or Date object.

BTW, if you don't need special objects, and want to preserve typing in TypeScript, you can

export function clone<T>(a: T): T {
  return JSON.parse(JSON.stringify(a));