javascript deepClone

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript deepClone相关的知识,希望对你有一定的参考价值。

const deepClone = obj => {
  let clone = Object.assign({}, obj);
  Object.keys(clone).forEach(
    key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
  );
  return Array.isArray(obj) && obj.length
    ? (clone.length = obj.length) && Array.from(clone)
    : Array.isArray(obj)
      ? Array.from(obj)
      : clone;
};


// Examples : 

const a = { foo: 'bar', obj: { a: 1, b: 2 } };
const b = deepClone(a); // a !== b, a.obj !== b.obj

以上是关于javascript deepClone的主要内容,如果未能解决你的问题,请参考以下文章

javascript深度克隆函数deepClone

编写 javascript 深度克隆函数 deepClone

deepClone

DataTable.Copy() 或 DeepClone。选择哪一个?

javascript 深度克隆Vue对象数据

vuex退出登录数据重置