【发布时间】: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