I'm pretty depressed because of this crazy problem. I have a difficult data structure composed of objects and arrays. It looks like this:
Team{
name: "name_of_team",
players: [
// PLAYER 1
{
id: 0,
position: 0,
track:[
// POINT 1
{
time: 0,
checked: false
},
// POINT 2
{
time: 0,
checked: false
},
// POINT 3
{
time: 0,
checked: false
}
]
},
// PLAYER 2
{
id: 1,
position: 0,
track:[
// POINT 1
{
time: 0,
checked: false
},
// POINT 2
{
time: 0,
checked: false
},
// POINT 3
{
time: 0,
checked: false
}
]
}
]
}
So I can track players. When one from team gets to point, its parameter checked changes to true and time saves the time of getting this zone. My function looks like this:
var a = Team.players.findIndex(x => x.id === 0);
var position = Team.players[a].position;
Team.players[a].track[position].checked = true;
Team.players[a].track[position].time = new Date();
Team.players[a].position++;
When I check parameter "a" it's set to 0 (right value) and it all seems good. But "checked" and "time" parameter changes also in the second object (id == 1) in this array. Crazy thing is that "position" parameter is correctly incremented only in the right object (id == 0). I tried to hard index the item in array like:
Team.players[0].track...
But it behaves the same. Has anyone experienced something like this or any ideas how to avoid second object being modified together with first object.
Thanks for any suggestions.
positiondefined ? Also there are no associative arrays in js so it would help if you provide a valid data examplePlayer1andPlayer2are supposed to be an index in an array. What are they?