【问题标题】:How to remove array element from an array of arrays which also has non array elements如何从也具有非数组元素的数组中删除数组元素
【发布时间】:2020-07-09 02:28:06
【问题描述】:

我正在开发一款多人游戏,并且我有一个具有以下布局的房间阵列(我添加了 cmets 以便更好地理解):

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

【问题讨论】:

  • 您选择这种数组结构而不是像 json 对象这样的东西有什么原因吗?这会让你更简单。
  • @PaulRyan 我通常使用 json 来存储数据,需要数组,因为对它们进行很多操作非常快。我不是 100% 的性能差异,而且该项目现在非常复杂,无法在这方面进行任何更改。

标签: javascript arrays splice array-splice


【解决方案1】:

这是一个改变初始数组的有效解决方案。

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

【讨论】:

  • 非常感谢您的帮助!它适用于一个房间,但是一旦有更多房间,它就会从所有子数组中删除相同的索引。我已经修复了它,但我担心它是一个糟糕的补丁。如果你想看看你可以检查这个小提琴:jsfiddle.net/jefawk/f8epo0ny TLDR:添加if (index &gt; 0) { players.splice(index, 1); }
  • 这不是一个糟糕的补丁,作为“找不到播放器”的条件很好的解决方案
  • if(index > -1) 是正确的,我不知道为什么我讨厌 0
  • 我没有注意到你写了if (index &gt; 0),正确的条件是if (index &gt; -1),因为0是数组索引的有效值
【解决方案2】:

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

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

【讨论】:

  • 感谢您的帮助!就像 Guerric P 一样,如果有更多房间,元素索引将从所有子数组中消失。我拿了他的代码并对照 -1 检查了索引,然后将其删除,可能也可以根据您发布的内容完成。再次感谢 :) !
【解决方案3】:

如果您想改为使用 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-12
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    相关资源
    最近更新 更多