javascript - Angular 8:数组 .map() .slice() 无功能
问题描述
带有 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']);
}
}
});
}
控制台日志:
(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 = data.operationsList.map((x) => Object.assign({}, x, { bank: 'Popular' }));
推荐阅读
- java - 如何使用 Spring Security 和 Webflux 忽略特定路径
- javascript - 如何在 Javascript 中计算多维数组中的相似值
- node.js - 护照-bnet InternalOAuthError:无法获取用户ID
- html - 图片链接只能部分点击
- php - 为什么 ZipArchive setEncryptionName 不起作用?
- javascript - drawStar() 与画布内的鼠标 mouse up mouse down
- kotlin - 如何在不同的端口上提供不同的 Ktor 模块?
- javascript - 如何从那个特殊的 json 中获取数据?
- kubernetes - 在 k8s 集群上部署 Hello World
- windows - 在另一台计算机上打开 Windows 性能监控文件 .blg