javascript - Angular 8 : Array . map() .slice() 没有函数

标签 javascript arrays angular typescript firebase

Angular 8 with Firebase: Array .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, …}

修改复制的数组时,更改也会反射(reflect)在原始数组中。

最佳答案

通过复制数组,您仍然保留对相同对象的引用。所以你需要更深入一层:

const temp = data.operationsList.map((x) => Object.assign({}, x, { bank: 'Popular' }));

关于javascript - Angular 8 : Array . map() .slice() 没有函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57795459/

相关文章:

javascript - 如何在 asp-route-id 前缀中传递 javaScript 变量值?

javascript - image onclick时无法显示图片

php - 在php中从mysql简单格式化多维数组

javascript - 如何以 Angular 查找给定数组列表的所有行中是否存在相同的值

reactjs - 为 React、Angular 等使用 HTTP 服务器

javascript - Xui.js 父对象

javascript - 从 firebase 和唯一键检索数据

javascript - 文档数据库 : How to filter document on array within array?

javascript - 解析 JSON 时未调用数据对象构造函数

javascript - 选择不是特定选择的子元素的元素