I am working on a chat and using an array to hold the users. Here is my problem:
我正在处理一个聊天,并使用一个数组来保存用户。这是我的问题:
User1 joins and is given Index 0 in the array via push. User2 joins and is given Index 1 in the array via push.
User1连接并通过push在数组中给出索引0。User2连接并通过push在数组中获得索引1。
User1 disconnects and is removed via splice.
User1断开连接,通过剪接删除。
NOW User2 becomes Index 0.
现在User2变成了索引0。
User1 reconnects and is given Index 1 via push.
User1重新连接,并通过push获得索引1。
User2 disconnects, and Index 1 is removed which is now User1.
User2断开连接,索引1被删除,现在是User1。
This is of course causing a problem.
这当然会引起问题。
So my question is how can I remove the item from the array without the index of the other elements changing? Am I on the wrong track here?
我的问题是,如何在不改变其他元素的索引的情况下,从数组中删除项目?我走错路了吗?
4 个解决方案
#1
11
Instead of removing the items from the array with splice()
, why not just set the value to null
or undefined
?
与其使用splice()从数组中删除项,不如将值设置为null或undefined?
Then when you're adding a new user, you can just scan through the array to find the first available slot.
然后,当您添加一个新用户时,您只需扫描数组以找到第一个可用的插槽。
javascript arrays are simply lists of items - they're not keyed to a specific key like you might be familiar with in PHP. So if you want to keep the same position in the array, you can't remove other items - you need to keep them, and just mark them as empty.
javascript数组仅仅是项目列表——它们不像PHP中常见的键那样被键化到特定的键上。所以如果你想在数组中保持相同的位置,你不能删除其他项——你需要保留它们,把它们标记为空。
You might scan through something like this:
你可以这样浏览一下:
var users = [];
function addUser(user) {
var id = users.indexOf(null);
if (id > -1) {
// found an empty slot - use that
users[id] = user;
return id;
} else {
// no empty slots found, add to the end and return the index
users.push(user);
return users.length - 1;
}
}
function removeUser(id) {
users[id] = null;
}
#2
4
Another option is to use a javascript object instead of an array.
另一种选择是使用javascript对象而不是数组。
Something like this:
是这样的:
var users = {};
users[1] = 'user 1';
users[2] = 'user 2';
delete users[1];
alert(users[2]); // alerts "user 2"
alert(typeof users[1]); // alerts "undefined"
You lose the array length
property though, so you'll have to keep track of your max user number yourself.
但是你失去了数组长度属性,所以你必须自己跟踪你的最大用户号。
#3
4
Use delete
instead of splice
.
使用delete而不是splice。
> a = ['1', '2', '3']
< Array [ "1", "2", "3" ]
> delete a[1]
< true
> a
< Array [ "1", undefined × 1, "3" ]
> a.length
< 3
#4
1
remove array elements without facing re-indexing problem
删除数组元素而不面临重新索引问题
var ind=[1,6]; //index positions of elements to remove
var arr=['a','b','c','d','e','f','g']; // actual array
var arr2 = arr.filter(function(item,index){
if(ind.indexOf(index)== -1){
return true;
}});
now arr2 is ==========>> ['a','c','d','e','f']
现在arr2 ==========>> ['a','c','d','e','f']
#1
11
Instead of removing the items from the array with splice()
, why not just set the value to null
or undefined
?
与其使用splice()从数组中删除项,不如将值设置为null或undefined?
Then when you're adding a new user, you can just scan through the array to find the first available slot.
然后,当您添加一个新用户时,您只需扫描数组以找到第一个可用的插槽。
javascript arrays are simply lists of items - they're not keyed to a specific key like you might be familiar with in PHP. So if you want to keep the same position in the array, you can't remove other items - you need to keep them, and just mark them as empty.
javascript数组仅仅是项目列表——它们不像PHP中常见的键那样被键化到特定的键上。所以如果你想在数组中保持相同的位置,你不能删除其他项——你需要保留它们,把它们标记为空。
You might scan through something like this:
你可以这样浏览一下:
var users = [];
function addUser(user) {
var id = users.indexOf(null);
if (id > -1) {
// found an empty slot - use that
users[id] = user;
return id;
} else {
// no empty slots found, add to the end and return the index
users.push(user);
return users.length - 1;
}
}
function removeUser(id) {
users[id] = null;
}
#2
4
Another option is to use a javascript object instead of an array.
另一种选择是使用javascript对象而不是数组。
Something like this:
是这样的:
var users = {};
users[1] = 'user 1';
users[2] = 'user 2';
delete users[1];
alert(users[2]); // alerts "user 2"
alert(typeof users[1]); // alerts "undefined"
You lose the array length
property though, so you'll have to keep track of your max user number yourself.
但是你失去了数组长度属性,所以你必须自己跟踪你的最大用户号。
#3
4
Use delete
instead of splice
.
使用delete而不是splice。
> a = ['1', '2', '3']
< Array [ "1", "2", "3" ]
> delete a[1]
< true
> a
< Array [ "1", undefined × 1, "3" ]
> a.length
< 3
#4
1
remove array elements without facing re-indexing problem
删除数组元素而不面临重新索引问题
var ind=[1,6]; //index positions of elements to remove
var arr=['a','b','c','d','e','f','g']; // actual array
var arr2 = arr.filter(function(item,index){
if(ind.indexOf(index)== -1){
return true;
}});
now arr2 is ==========>> ['a','c','d','e','f']
现在arr2 ==========>> ['a','c','d','e','f']