Jonah
Jonah

Reputation: 1545

Removing one item from multiple identical arrays

I am trying to remove items from arrays that are all the same. Interestingly, it works when my arrays are different. The following code works as intended, removing the Fireman from team1, the Pyromancer from team2, the Aronist from team3, and the Vigilante from team4.

var options = {};
options.characters = ["Fireman","Missionary","Vigilante","Corrupt Policeman","Suicide Bomber","Arsonist","Gunman","Pyromancer","Hypnotist","Picklock","Policeman","Child","Prostitute"];
this._team1Characters = options.characters.concat(["test1"]);
this._team2Characters = options.characters.concat(["test2"]);
this._team3Characters = options.characters.concat(["test3"]);
this._team4Characters = options.characters.concat(["test4"]);
var submissions = {};
submissions.character1 = "Fireman";
submissions.character2 = "Pyromancer";
submissions.character3 = "Arsonist";
submissions.character4 = "Vigilante";
var i = 5;
    while(--i){

        if(submissions["character"+i]){ 

            this["_team"+i+"Characters"].splice( this["_team"+i+"Characters"].indexOf(submissions["character"+i]), 1 );            
        }
    }
alert(this._team1Characters);
alert(this._team2Characters);
alert(this._team3Characters);
alert(this._team4Characters);

However, if the four arrays are the same, all arrays end up the same (all lack the Fireman, the Pyromancer, the Aronist and the Vigilante). This is the code causing the problem:

var options = {};
options.characters = ["Fireman","Missionary","Vigilante","Corrupt Policeman","Suicide Bomber","Arsonist","Gunman","Pyromancer","Hypnotist","Picklock","Policeman","Child","Prostitute"];
this._team1Characters = options.characters; 
this._team2Characters = options.characters;
this._team3Characters = options.characters;
this._team4Characters = options.characters;
var submissions = {};
submissions.character1 = "Fireman";
submissions.character2 = "Pyromancer";
submissions.character3 = "Arsonist";
submissions.character4 = "Vigilante";
var i = 5;
    while(--i){

        if(submissions["character"+i]){ 

            this["_team"+i+"Characters"].splice( this["_team"+i+"Characters"].indexOf(submissions["character"+i]), 1 );
        }
    }
alert(this._team1Characters);
alert(this._team2Characters);
alert(this._team3Characters);
alert(this._team4Characters);

What's the reason for this?

Upvotes: 2

Views: 83

Answers (2)

hon2a
hon2a

Reputation: 7214

When you do this._team1Characters = options.characters; you're passing a reference, so you really end up with lots of references to the same array. Consequently, when you change that single array, you'll see the change through any of the references.

If you want to copy an array, use Array.prototype.slice().

this._team1Characters = options.characters.slice();
...

Upvotes: 3

gurvinder372
gurvinder372

Reputation: 68443

since you are assigning the reference of the same variable to all, so all the removals are essentially happening from same array options.characters

try doing it this way

this._team1Characters = options.characters.concat( [] ); 
this._team2Characters = options.characters.concat( [] );
this._team3Characters = options.characters.concat( [] );
this._team4Characters = options.characters.concat( [] );

this will ensure that all get the same values in a different array and values in other arrays are not affected when removed from one array

Upvotes: 2

Related Questions