在Angular 2 + TypeScript中深层复制数组 [英] Deep copy an array in Angular 2 + TypeScript
本文介绍了在Angular 2 + TypeScript中深层复制数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个输入对象数组。让我们称之为内容
。
I have an array of objects that is an input. Lets call it content
.
当尝试深度复制它时,它仍然具有对前一个数组的引用。
When trying to deep copy it, it still has a reference to the previous array.
我需要复制该输入数组,并更改重复部分的一个属性。
I need to duplicate that input array, and change one property of the duplicated part.
很久以来我尝试了不同的方法。
So long I've tried different methods that weren't successful.
ES6方式:
public duplicateArray() {
arr = [...this.content]
arr.map((x) => {x.status = DEFAULT});
return this.content.concat(arr);
}
切片
方式:
public duplicateArray() {
arr = this.content.slice(0);
arr.map((x) => {x.status = DEFAULT});
return this.content.concat(arr);
}
在这两个数组中,数组中的所有对象都有 status:'Default'
。
In both of them all the objects inside the array have status: 'Default'
.
在Angular 2中深度复制数组的最佳方法是什么?
What's the best approach to deep copy the array in Angular 2?
推荐答案
选中此项:
let cloned = source.map(x => Object.assign({}, x));
这篇关于在Angular 2 + TypeScript中深层复制数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文