我是泰里尼格要克隆的对象,你知道这个对象是否存在更好的解决方案?
this.objClone = JSON.parse(JSON.stringify(this.obj));
我正在使用redux,但出现此错误:
错误TypeError:无法分配为只读对象的属性“值” '[object Object]'
答案 0 :(得分:1)
尝试使用
this.objClone = Object.assign({}, this.obj);
您还可以使用use lodash:
对于许多对象/数组操作,建议使用lodash
可能的Cloning objects TypeScript,What's alternative to angular.copy in Angular或What is the most efficient way to deep clone an object in JavaScript?副本
答案 1 :(得分:0)
您可以使用Lodash的cloneDeep,但JSON克隆的性能更好
答案 2 :(得分:0)
您可以使用spread operator。
let original = {value: 'test'};
let copy = {...original};
copy.value = 'new test';
console.log(original.value)// -> 'test'
console.log(copy.value) // -> 'new test'