1

Angular 8 with Firebase: Array .map () .slice () Do not work. Hi. I have been trying to make a copy of an array using .map () and .slice () in Angular 8, but using these methods I find that the copied array still has the reference to the original array.

I don't know if I'm doing it wrong, or these methods don't work in 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, …}

When modifying the copied array, the changes are also reflected in the original array.

3
  • If you're copying the array already, why not just use const temp = this.userData.operationList.map(...) for your map? Commented Sep 4, 2019 at 20:53
  • Not a direct duplicate, but has the same idea. stackoverflow.com/q/52085758/575527 . Object.assign() only does a "shallow clone". Commented Sep 4, 2019 at 20:57
  • @Jbluehdorn I already tried it that way, and the result is the same. Commented Sep 4, 2019 at 20:59

2 Answers 2

2

By copying an array, you're still keeping references to the same objects. So you need to go one level deeper:

const temp = data.operationsList.map((x) => Object.assign({}, x, { bank: 'Popular' }));
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it worked for me. What I don't understand is why in javascritp the .map () function works fine, but using angular and firebase the reference to the original array is maintained.
Angular has nothing to do with it. If you map or slice an array of primitives (like numbers, strings or Booleans), you're fine, you're creating a legit, independent copy. With an array of objects you need to be a little more careful.
Wr8. Still, if you want to assign value to array, then try using Spread Operator
0

You're modifying a direct reference to the underlying data directly within your map. Change it to something like:

let tempX = Object.assign({}, x);
tempX.bank = 'Popular';
return tempX;

1 Comment

It did not work, still maintaining the reference to the original array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.