Answer the question
In order to leave comments, you need to log in
How to remove an element from an associative array in nodejs?
Good afternoon.
Brief gist. I want to make a block on the site, with users who are currently on the site.
Implementation. I send data about the user to the server via sockets. There I fill everything in one single array and already update it for all users.
Creating an array. "Repeat checks skipped"
var arr_online_user = [];
arr_online_user["user"] = new Array();
arr_online_user["user"] = new Object();
arr_online_user["user"].userid = userid;
arr_online_user["user"].userip = userip;
arr_online_user["user"].avatar = avatar;
arr_online_user["user"].name = name;
onlineuser = arr_online_user["user"];
checkArrGlobal = checkArrGlobal.concat(onlineuser);
[{"userid":"#","userip":"111.11.111.111","avatar":"img/incognito.png","name":"Не авторизован"},{"userid":"3054895","userip":"222.222.22.222","avatar":"public/images/avatars/c0/c93a5b.jpg","name":"Haruhi Suzumiya"},{"userid":"3054896","userip":"333.222.22.222","avatar":"public/images/avatars/c0/c0e9efc389f.jpg","name":"Haruhi"},{"userid":"305","userip":"222.333.22.222","avatar":"public/images/avatars/c0/c0e0fa8eefc3.jpg","name":"Suzumiya"},{"userid":"895","userip":"222.222.333.222","avatar":"public/images/avatars/c0/c0b6055.jpg","name":"IUGIUIh"}]
for (var j = 0; j < checkArrGlobal.length; j++) {
if(checkArrGlobal[j].userip.indexOf(data.userip) == -1){
console.log('>>>> Не тот '+data.userip+' == '+JSON.stringify(checkArrGlobal[j].userip));
}else{
//console.log('>>>> Найден пользователь '+data.userip+' ='+j+'= '+JSON.stringify(checkArrGlobal[j].userip));
console.log('>>>> По ключу ='+j+'= содержит '+JSON.stringify(checkArrGlobal[j]));
delete checkArrGlobal[j];
onlineuser_json = JSON.stringify(checkArrGlobal);
break;
}
}
Пользовательне найдей в массиве по userip 111.11.111.111
onlineuser_json содержит [null,{"userid":"29040","userip":"111.11.111.111","avatar":"2d3e71e27e3ea972cf9e562.jpg","name":"ski"},{"userid":"4486546","userip":"222.22.22.22","avatar":"4f1e3bd549c4d45ad3.jpg","name":"Olden"}]
Answer the question
In order to leave comments, you need to log in
js doesn't have associative arrays. They are replaced by objects. Delete property of object - operator delete
In js there are simply arrays. Applying delete to an array element will remove it, but will not change the length property of the array.
And the size of an ordinary array is equal to the length property, when it is displayed, the missing elements will be present with an empty value.
ar=[];
ar.length=5;
console.log(ar); // [ , , , , ]
console.log("0" in ar); // false
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question