【发布时间】:2019-09-04 20:48:55
【问题描述】:
带有 Firebase 的 Angular 8:数组 .map () .slice () 不起作用。 你好。 我一直在尝试使用 Angular 8 中的 .map () 和 .slice () 来制作数组的副本,但是使用这些方法我发现复制的数组仍然具有对原始数组的引用。
我不知道是我做错了,还是这些方法在 Angular 8 中不起作用。
// Iniciamos la escucha de los cambios de la data del usuario
if (!this.$userData) {
this.$userData = this.userService.user().valueChanges().subscribe(data => {
if (data) {
this.userData = Object.assign({}, data);
const temp = data.operationsList.map((x) => {
x.bank = 'Popular';
return x;
});
console.log(this.userData.operationsList, temp);
if (!this.userData.validated) {
this.router.navigate(['register/pendingValidation']);
}
}
});
}
console.log:
(2) [{…}, {…}]
0: {bank: "Popular", commission: 0, country: "ve", countryAllowed: "all", maximum: 0, …}
1: {bank: "Popular", commission: 0, country: "ve", countryAllowed: "all", maximum: 0, …}
(2) [{…}, {…}]
0: {bank: "Popular", commission: 0, country: "ve", countryAllowed: "all", maximum: 0, …}
1: {bank: "Popular", commission: 0, country: "ve", countryAllowed: "all", maximum: 0, …}
在修改复制的数组时,更改也会反映在原始数组中。
【问题讨论】:
-
如果您已经在复制数组,为什么不直接使用
const temp = this.userData.operationList.map(...)来制作地图? -
不是直接复制,但有相同的想法。 stackoverflow.com/q/52085758/575527 。
Object.assign()只做“浅层克隆”。 -
@Jbluehdorn 我已经试过了,结果是一样的。
标签: javascript arrays angular typescript firebase