Ömer Faruk Arslan Ömer Faruk Arslan - 3 years ago 65
Javascript Question

Delete json data in array

I am trying to remove a piece of a data from a json array. For example I have this array;

var users = [];
io.on('connection',function(socket){
socket.on('nickname', function(nick){
socket.nick = nick;
users.push({
user:socket.nick,
userid:socket.id,
socket:socket
});
});


I am trying to delete in users this way;

socket.on('disconnect', function(){
delete users[{
user,
userid,
socket
}];
});
});


How can ı do that?

Answer Source

You may try using lodash methods to achieve what you are trying to: -

var _ = require('lodash');
socket.on('disconnect', function(){
        _.pullAllWith(users, [{
             user:socket.nick,
             userid:socket.id,
             socket:socket
             }], _.isEqual)
       });
     });

see if this works..

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download