如何从也具有非数组元素的数组数组中删除数组元素

问题描述 投票:0回答:3

我正在从事多人游戏,并且我的房间阵列具有以下布局(我添加了注释以便更好地理解):

Room_Array
[
    [
        "Room_0",   // room name
        10,         // max people
        "Random",   // room type
        [           // now arrays of players follow
            [
               1,     // ShortID
               123,   // position X
               234,   // position Y
               10     // angle
            ],
            [
               2,
               123,
               234,
               10
            ],
            [
               3,
               123,
               234,
               10
            ],
        ]
    ]
    // here other rooms are created with the same layout as Room_0 when the max people is reached
]

我将如何删除ShortID = 2的播放器的整个阵列?万一他断开连接?

所以期望的结果将是:

Room_Array
[
    [
        "Room_0",   // room name
        10,         // max people
        "Random",   // room type
        [           // now arrays of players follow
            [
               1,     // ShortID
               123,   // position X
               234,   // position Y
               10     // angle
            ],
            [
               3,
               123,
               234,
               10
            ],
        ]
    ]
]

我尝试了以下代码,并在控制台日志中向我显示了我需要拼接的数组元素,因此,2,123,234,10。注释的拼接导致错误的未识别元素1。

for (var i = 0; i < Room_Array.length; i++)
{
    if (Room_Array[i][0] === PlayerObject[socket.id].RoomName)
    {
        for (var j = 0; j < Room_Array[i][3].length; j++)
        {
            if (Room_Array[i][3][j][0] === PlayerObject[socket.id].ShortID)
            {
                console.log("Array to splice: " + Room_Array[i][3][j]);
                //Room_Array.splice([i][3][j], 1); // error unidentified 1

            }
        }


    break;
    }
}
javascript arrays splice array-splice
3个回答
0
投票

这里是对初始数组进行变异的有效解决方案。

const Room_Array = [
    [
        "Room_0",   // room name
        10,         // max people
        "Random",   // room type
        [           // now arrays of players follow
            [
               1,     // ShortID
               123,   // position X
               234,   // position Y
               10     // angle
            ],
            [
               2,     // ShortID
               123,   // position X
               234,   // position Y
               10     // angle
            ],
            [
               3,
               123,
               234,
               10
            ],
        ]
    ]
];
    
function removeUser (array, id) {
  array.forEach(room => {
    const [roomName, maxPeople, roomType, players] = room;
    const player = players.find(([shortId, ...others]) => shortId === id);
    const index = players.indexOf(player);
    players.splice(index, 1);
  });
}

removeUser(Room_Array, 2);

console.log(Room_Array);
    

0
投票

let Room_Array=[["Room_0", 10, "Random",[[ 1,123,234,10],[2,123,234,10],[3,123,234, 10]],]];
function remove(id){
return Room_Array.map( room => {room[3].splice(room[3].findIndex( rm=>rm[0]==id),1);return room;})
}
console.log(remove(2));
.as-console-wrapper { max-height: 100% !important; top: 0; }

0
投票

我强烈建议您改用JSON路由,这是一个有效的示例:

let Room_Array = 
[
    {
        roomName: "Room_0",
        maxPeope: 10,
        roomType: "Random",
        players: [
            {
               shortID: 1,
               xPosition: 123,
               yPosition: 234, 
               angle: 10
            },
            {
               shortID: 2,
               xPosition: 123,
               yPosition: 234, 
               angle: 10
            },
            {
               shortID: 3,
               xPosition: 123,
               yPosition: 234, 
               angle: 10
            },
        ]
    }
];

function removeUser(id)
{
  Room_Array = Room_Array.map((room) => 
  {
    room.players = room.players.filter(player => player.shortID !== id);
    return room;
  });
}
© www.soinside.com 2019 - 2024. All rights reserved.