简体   繁体   English

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

[英]How to remove array element from an array of arrays which also has non array elements

I am working on a multiplayer game and I have a room array with the following layout (I added comments for better understanding):我正在开发一个多人游戏,我有一个具有以下布局的房间阵列(我添加了注释以便更好地理解):

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
]

How would I go around to remove the whole array of the player with ShortID = 2?我将如何使用 ShortID = 2 移除整个播放器数组? in case he disconnects?万一他断开连接?

So the desired result would be:所以想要的结果是:

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
            ],
        ]
    ]
]

I tried the following code and in the console log it showed me the elements of the array I need to splice, so 2, 123, 234, 10. The commented splice resulted in error unidentified element 1.我尝试了以下代码,并在控制台日志中向我显示了我需要拼接的数组元素,因此为 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;
    }
}

Here is a working solution that mutates the initial array.这是一个改变初始数组的工作解决方案。

 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 index = players.findIndex(([shortId]) => shortId === id); if(index > -1) { players.splice(index, 1); } }); } removeUser(Room_Array, 2); console.log(Room_Array);

Using foreach modifying the existing array使用foreach修改现有数组

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

Using map returning a new array使用 map 返回一个新数组

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

If you want to go down the JSON route instead, which I highly recommend, here's a working example:如果您想使用我强烈推荐的 JSON 路由,这里有一个工作示例:

 const 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.forEach((room) => { room.players = room.players.filter(player => player.shortID !== id); }); console.log(Room_Array); } removeUser(1);

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

相关问题 从数组数组中删除元素 - Remove element from an array of arrays 如何从数组中删除哪些元素是另一个数组的元素? - How can remove elements from array which elements are another array? Javascript - 如果元素的父元素中的 id 也在数组中,则从数组中删除元素 - Javascript - remove element from an array if the id in parent of an element is also in the array 如何获取包含一些也是数组的元素的JS数组,并使所有数组元素成为顶级元素 - How do I take a JS array that has some elements that are also arrays and make ALL array elements top level elements 推入具有数组数组的数组数组 - Push into an array of arrays which has an array of arrays 如何使用 GAS 从数组的 arrays 中删除多个元素? - How to remove multiple elements from arrays of an array using GAS? 从数组数组中删除数组 - Remove an array from an array of arrays 如何比较两个不同对象内的 arrays 的元素并显示哪个元素属于 object 内的哪个数组 - how to compare elements of arrays inside two different objects and display which element belonged to which array that was inside the object 如何从作为嵌套对象的数组中删除元素 - How to remove element from an array which as nested objects 如果数组具有多个“ a”元素,则从数组中删除单个“ a”值的重复项 - Remove duplicates of single “a” value from array if array has multiple “a” elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM