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...
Object.assign(this.pickAccounts , this.accounts);...mapGetters('accounts', ['accounts'])should be...mapGetters(['accounts'])this.pickAccounts = this.accounts.slice(0)to clone thethis.accountsarray. Your approach is assigningthis.accountsby reference tothis.pickAccounts, so when you changeaccountsorpickAccountswithsplice(0, 1), it changes both.this.pickAccounts = this.accounts.slice(0). Noteslice(0)