S
S
Sergey2017-05-24 09:21:33
JavaScript
Sergey, 2017-05-24 09:21:33

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

The content of the array.
[{"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"}]

When deleting (closing the tab), data is also sent to the server with user data. If the user was authorized, then I try to find and delete by userid , if it was a guest, then by userip.
Search for the element to be deleted (implementation only by ip).
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;
          
        }
      }

Content of onlineuser_json after deletion.
Пользовательне найдей  в массиве по 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"}]

As you can see, the first element of the array is null, how to correctly remove data from the array so that this does not happen. Due to null, nodejs throws an error and the module itself on the site stops working.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
rustler2000, 2017-05-24
@interests70

If array - then ```checkArrGlobal.splice(j, 1)```

E
Egor Zhivagin, 2017-05-24
@Krasnodar_etc

Watch here . There's an explanation as to why.

A
Anton Shvets, 2017-05-24
@Xuxicheta

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

splice makes sure that the length of the array is up to date with its contents and respects the order of the indexes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question