简体   繁体   English

Javascript 更改对象数组中所有项目的属性

[英]Javascript changes attributes in all items in array of objects

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.当团队中的一个到达点时,其参数检查更改为true,并且时间节省了获取该区域的时间。 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.当我检查参数“a”时,它被设置为 0(正确的值),看起来一切都很好。 But "checked" and "time" parameter changes also in the second object (id == 1) in this array.但是“已检查”和“时间”参数也在此数组中的第二个对象 (id == 1) 中更改。 Crazy thing is that "position" parameter is correctly incremented only in the right object (id == 0).疯狂的是,“位置”参数仅在正确的对象(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.感谢您的任何建议。

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++;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM