2

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.

7
  • where is position defined ? Also there are no associative arrays in js so it would help if you provide a valid data example Commented Oct 13, 2017 at 8:56
  • 1
    Uhm, how do you initialize the Objects inside the track array.. this smells heavlily like you're having side-effects because the point object in the arrays is the same object Commented Oct 13, 2017 at 8:58
  • I'm sorry. That should be position = Team.players[a].position; Commented Oct 13, 2017 at 9:01
  • Player1 and Player2 are supposed to be an index in an array. What are they? Commented Oct 13, 2017 at 9:01
  • Objects are copied into an array. I create a track array elsewhere and then I copy it to player.track. When players are from the same team then track array is copied to both player.trace parameters. Commented Oct 13, 2017 at 9:04

1 Answer 1

1

Assuming

Team.players[a].position

is the position, then just use it.

var a = Team.players.findIndex(x => x.id === 0);
Team.players[a].track[Team.players[a].position].checked = true;
//                    ^^^^^^^^^^^^^^^^^^^^^^^^
Team.players[a].track[Team.players[a].position].time = new Date();
//                    ^^^^^^^^^^^^^^^^^^^^^^^^
Team.players[a].position++;
Sign up to request clarification or add additional context in comments.

1 Comment

I already fixed the code. I've written it wrong so I apologize. Could you please have a look at it now?

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.