0

I'm getting very weird result if I want to remove matched elements of object of arrays

The code snippet is here;

computed;

...mapGetters('accounts', ['accounts'])  

info: this.accounts setted with mapAction in created() and it's an array and length is 6

methods;

     assignEvent(teammate){

      this.pickAccounts = this.accounts;

      this.pickAccounts.splice(0, 1)

      this.assign.teammate = teammate;
      this.show.assign = true;

    }

step 1 - assignEvent() fired then this.pickAccounts.length reduced from 6 to 5

it's ok but;

step 2 - Let's Trigger assignEvent() again I assume that going to be same thing again 6 to 5 because I assume that If I set;

this.pickAccounts = this.accounts;

this.pickAccounts going to be equal to this.accounts but it's not.

If I trigger this function (assignEvent()) this.accounts is also changing.

How this.accounts is changing? I'm getting this.accounts from vuex store I'm not changing it's value it must be stay same...

7
  • 1
    try Object.assign(this.pickAccounts , this.accounts); Commented Jan 23, 2019 at 0:53
  • Also, ...mapGetters('accounts', ['accounts']) should be ...mapGetters(['accounts']) Commented Jan 23, 2019 at 0:55
  • 2
    Also, use this.pickAccounts = this.accounts.slice(0) to clone the this.accounts array. Your approach is assigning this.accounts by reference to this.pickAccounts, so when you change accounts or pickAccounts with splice(0, 1), it changes both. Commented Jan 23, 2019 at 0:58
  • How Can I avoid this updating of this .accounts? I want to get constant data everytime from this.accounts! I don't want to manipulate it Commented Jan 23, 2019 at 1:04
  • 1
    this.pickAccounts = this.accounts.slice(0). Note slice(0) Commented Jan 23, 2019 at 1:08

0

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.