How to write to this whois json file properly without doing the same thing over and over again?

Ok so I know programming rule number 1 is if you have to do it more than once script it.

However I'm kind of stuck at this point and would like someone to help assist me as I keep doing multiple filters.

Here is an example of the json file:

[{
    "ids": [{
        "room": "i3ryok",
        "id": ["44314561"]
    }],
    "sessions": [{
        "room": "i3ryok",
        "session": ["79603616"]
    }],
    "names": [],
    "temps": [],
    "anons": [{
        "name": "_anon5952",
        "rooms": ["i3ryok"]
    }],
    "ips": []
}, {
    "ids": [{
        "room": "i3ryok",
        "id": ["44314562"]
    }],
    "sessions": [{
        "room": "i3ryok",
        "session": ["79603619"]
    }],
    "names": [],
    "temps": [],
    "anons": [{
        "name": "_anon5953",
        "rooms": ["i3ryok"]
    }],
    "ips": []
}]

And this is my code which im extremely sure a bunch of you will look at this and be like what have you done this is the biggest fail code ever

DerpLib.events.on('participant', function(room, req){
    var db = DerpLib.MM.plugin.load('database');
    if(_.filter(db.get('','whois'), function(x){ return x; }).length === 0){
        db.get('', 'whois').push({'ids': [{'room': room.name, 'id': [req.user.id]}], 'sessions': [{'room': room.name, 'session': [req.user.sess]}], 'names': (['login', 'leave-user', 'join-user'].indexOf(req.mode) !== -1 ? [{'name': req.user.name, 'rooms': [room.name]}] : []), 'temps': (['temp', 'leave-temp', 'join-temp'].indexOf(req.mode) !== -1 ? [{'name': req.user.alias, 'rooms': [room.name]}] : []), 'anons': (['logout', 'leave-anon', 'join-anon'].indexOf(req.mode) !== -1 ? [{'name': req.user.name, 'rooms': [room.name]}] : []), 'ips': []});
    }else{
        _.each(db.get('','whois'), function(userobj, objnum){
            var m_ids = _.filter(userobj.ids, function(x){ return x.id.indexOf(req.user.id) !== -1; });
            var m_sess = _.filter(userobj.sessions, function(x){ return x.session.indexOf(req.user.sess) !== -1; });
            var m_names = _.filter(userobj.names, function(x){ return x.name === req.user.name; });
            var m_temps = _.filter(userobj.temps, function(x){ return x.name === req.user.alias; });
            var m_anons = _.filter(userobj.anons, function(x){ return x.name === req.user.name; });
            var u_ids = _.filter(userobj.ids, function(x){ return x.id.indexOf(req.user.id) === -1; });
            var u_sess = _.filter(userobj.sessions, function(x){ return x.session.indexOf(req.user.sess) === -1; });
            var u_names = _.filter(userobj.names, function(x){ return x.name !== req.user.name; });
            var u_temps = _.filter(userobj.temps, function(x){ return x.name !== req.user.alias; });
            var u_anons = _.filter(userobj.anons, function(x){ return x.name !== req.user.name; });
            if(m_ids.length === 1 && m_sess.length === 1 && u_ids.length >= 0 && u_sess.length >= 0){
                if(['leave-user','leave-temp','leave-anon'].indexOf(req.mode) !== -1){
                    if(m_ids[0].room === room.name){
                        db.get('','whois')[objnum].ids[db.get('','whois')[objnum].ids.indexOf(m_ids[0])].id.splice(db.get('','whois')[objnum].ids[db.get('','whois')[objnum].ids.indexOf(m_ids[0])].id.indexOf(req.user.id), 1);
                    }
                    if(m_sess[0].room === room.name){
                        db.get('','whois')[objnum].sessions[db.get('','whois')[objnum].sessions.indexOf(m_sess[0])].session.splice(db.get('','whois')[objnum].sessions[db.get('','whois')[objnum].sessions.indexOf(m_sess[0])].session.indexOf(req.user.sess), 1);
                    }
                }
                if(m_names.length === 1){
                    if(m_names.rooms.indexOf(room.name) === -1){
                        db.get('','whois')[objnum].names[db.get('','whois')[objnum].names.indexOf(m_names)].rooms.push(room.name);
                    }
                }else if(m_anons.length === 1){
                    if(m_anons.rooms.indexOf(room.name) === -1){
                        db.get('','whois')[objnum].anons[db.get('','whois')[objnum].anons.indexOf(m_anons)].rooms.push(room.name);
                    }
                }else if(m_temps.length === 1){
                    if(m_temps.rooms.indexOf(room.name) === -1){
                        db.get('','whois')[objnum].temps[db.get('','whois')[objnum].temps.indexOf(m_temps)].rooms.push(room.name);
                    }
                }else{
                    if(['login', 'leave-user', 'join-user'].indexOf(req.mode) !== -1){
                        if(m_names.length === 0){
                            db.get('','whois')[objnum].names.push({'name': req.user.name, 'rooms': [room.name]});
                        }
                    }else if(['temp', 'leave-temp', 'join-temp'].indexOf(req.mode) !== -1){
                        if(m_temps.length === 0){
                            db.get('','whois')[objnum].temps.push({'name': req.user.alias, 'rooms': [room.name]});
                        }
                    }else if(['logout', 'leave-anon', 'join-anon'].indexOf(req.mode) !== -1){
                        if(m_anons.length === 0){
                            db.get('','whois')[objnum].anons.push({'name': req.user.name, 'rooms': [room.name]});
                        }
                    }
                }
            }else if(m_ids.length === 1 && m_sess.length === 0 && u_ids.length >= 0 && u_sess.length >= 0){
                if(['leave-user','leave-temp','leave-anon'].indexOf(req.mode) !== -1){
                    if(m_ids[0].room === room.name){
                        db.get('','whois')[objnum].ids[db.get('','whois')[objnum].ids.indexOf(m_ids[0])].id.splice(db.get('','whois')[objnum].ids[db.get('','whois')[objnum].ids.indexOf(m_ids[0])].id.indexOf(req.user.id), 1);
                    }
                }
                if(m_names.length === 1){
                    if(m_names.rooms.indexOf(room.name) === -1){
                        db.get('','whois')[objnum].names[db.get('','whois')[objnum].names.indexOf(m_names)].rooms.push(room.name);
                    }
                    var sess = _.filter(u_sess, function(x){ return x.room === room.name; });
                    if(sess.length === 1){
                        if(['join-user', 'login'].indexOf(req.mode) !== -1){
                            db.get('','whois')[objnum].sessions[db.get('','whois')[objnum].sessions.indexOf(sess[0])].session.push(req.user.sess);
                        }else if(req.mode === 'leave-user'){
                            db.get('','whois')[objnum].sessions[db.get('','whois')[objnum].sessions.indexOf(sess[0])].session.splice(db.get('','whois')[objnum].sessions[db.get('','whois')[objnum].sessions.indexOf(sess[0])].session.indexOf(req.user.sess), 1);
                        }
                    }else if(sess.length === 0){
                        if(['join-user', 'login'].indexOf(req.mode) !== -1){
                            db.get('','whois')[objnum].sessions.push({'room': room.name, 'session': [req.user.sess]});
                        }else if(req.mode === 'leave-user'){
                            db.get('','whois')[objnum].sessions.push({'room': room.name, 'session': []});
                        }
                    }
                }else if(m_names.length === 0 && m_anons.length === 0 && m_temps.length === 0){
                    db.get('','whois').push({'ids': [{'room': room.name, 'id': [req.user.id]}], 'sessions': [{'room': room.name, 'session': [req.user.sess]}], 'names': (['login', 'leave-user', 'join-user'].indexOf(req.mode) !== -1 ? [{'name': req.user.name, 'rooms': [room.name]}] : []), 'temps': (['temp', 'leave-temp', 'join-temp'].indexOf(req.mode) !== -1 ? [{'name': req.user.alias, 'rooms': [room.name]}] : []), 'anons': (['logout', 'leave-anon', 'join-anon'].indexOf(req.mode) !== -1 ? [{'name': req.user.name, 'rooms': [room.name]}] : []), 'ips': []});
                }
            }else if(m_ids.length === 0 && m_sess.length === 0 && u_ids.length >= 0 && u_sess.length >= 0){
                if(m_names.length === 1){
                    if(m_names.rooms.indexOf(room.name) === -1){
                        db.get('','whois')[objnum].names[db.get('','whois')[objnum].names.indexOf(m_names)].rooms.push(room.name);
                    }
                    var ids = _.filter(u_ids, function(x){ return x.room === room.name; });
                    if(ids.length === 1){
                        if(['join-user', 'login'].indexOf(req.mode) !== -1){
                            db.get('','whois')[objnum].ids[db.get('','whois')[objnum].ids.indexOf(ids[0])].id.push(req.user.id);
                        }else if(req.mode === 'leave-user'){
                            db.get('','whois')[objnum].ids[db.get('','whois')[objnum].ids.indexOf(ids[0])].id.splice(db.get('','whois')[objnum].ids[db.get('','whois')[objnum].ids.indexOf(ids[0])].id.indexOf(req.user.id), 1);
                        }
                    }else if(ids.length === 0){
                        if(['join-user', 'login'].indexOf(req.mode) !== -1){
                            db.get('','whois')[objnum].ids.push({'room': room.name, 'id': [req.user.id]});
                        }else if(req.mode === 'leave-user'){
                            db.get('','whois')[objnum].ids.push({'room': room.name, 'id': []});
                        }
                    }
                    var sess = _.filter(u_sess, function(x){ return x.room === room.name; });
                    if(sess.length === 1){
                        if(['join-user', 'login'].indexOf(req.mode) !== -1){
                            db.get('','whois')[objnum].sessions[db.get('','whois')[objnum].sessions.indexOf(sess[0])].session.push(req.user.sess);
                        }else if(req.mode === 'leave-user'){
                            db.get('','whois')[objnum].sessions[db.get('','whois')[objnum].sessions.indexOf(sess[0])].session.splice(db.get('','whois')[objnum].sessions[db.get('','whois')[objnum].sessions.indexOf(sess[0])].session.indexOf(req.user.sess), 1);
                        }
                    }else if(sess.length === 0){
                        if(['join-user', 'login'].indexOf(req.mode) !== -1){
                            db.get('','whois')[objnum].sessions.push({'room': room.name, 'session': [req.user.sess]});
                        }else if(req.mode === 'leave-user'){
                            db.get('','whois')[objnum].sessions.push({'room': room.name, 'session': []});
                        }
                    }
                }else if(m_names.length === 0){
                    db.get('','whois').push({'ids': [{'room': room.name, 'id': [req.user.id]}], 'sessions': [{'room': room.name, 'session': [req.user.sess]}], 'names': (['login', 'leave-user', 'join-user'].indexOf(req.mode) !== -1 ? [{'name': req.user.name, 'rooms': [room.name]}] : []), 'temps': (['temp', 'leave-temp', 'join-temp'].indexOf(req.mode) !== -1 ? [{'name': req.user.alias, 'rooms': [room.name]}] : []), 'anons': (['logout', 'leave-anon', 'join-anon'].indexOf(req.mode) !== -1 ? [{'name': req.user.name, 'rooms': [room.name]}] : []), 'ips': []});
                }
            }
        });
    }
});

the above code is in main.js

my problem is that i am using filter from the underscore module WAY too much and dont know any other way to do what i want

rather my brain just doesn't want to think

for those that are writers as well as coders

well i like to call this coders block much like writers block

anyway back to the main problem

How do i write the above code properly without repeating filter over and over again like a noob?

If more info is needed i will be happy to provide it as the current code locks my program up and keeps the cpu load at 100% which is a big problem for me.

My above code is a chatango bot i made using the following library found on github at https://github.com/MakuraYami/derplib < its a bit outdated my version has more stuff

the code you are looking at above is the code for adding entries to the json based on id session and username

https://www.dropbox.com/s/sohi1ntpruxhaou/frame.js?dl=0 in my updated frame.js from lines 224 to 236 the participants event which the script is listening for (although not actually sent from here) take a look at that first to get the general idea

this info is sent by a chat room on chatango when ever a user joins leaves logs in or logs out of the chatroom

https://www.dropbox.com/s/f6zc3o6i9loy2vt/ch_room.js?dl=0 lines 300-315 you should see where the participants event is actually fired

also this is how the data is saved into the db or rather gzip file https://github.com/MakuraYami/derplib/blob/master/derplib/saving.js

database.js in the plugins folder https://www.dropbox.com/s/cjdpmt4vej48yj1/database.js?dl=0 my updated version

also i would like to note that derplib is barrykun AKA MakuraYami's Nodejs lib he is my friend we have spent a lot of time on it

from what i know so far the main reason for my horrific load problem is that i'm using filter way too much so i need to make a better method of trying to manage the json

my current code flow is like this

for the ids i record the room name and a list of ids each for everytime the same username is in the chat (on different devices) (to prevent other problems) into a object and i push a new object for each room that user is in when the user on a specific id leaves the room the id is spliced from the array

the same goes for sessions but unlike id which is only different per device sessions differ by tabs so each tab they have open no matter the device on that same user is a new session

for names temps and anons i record a name based on the user type and record each room that name has been in into a single object and add new objects of the same nature for each name

for ips although not shown here i can give an example of what it would look like

{
    "ips": [{
        "ip": "121.237.90.20",
        "names": ["_anon5953"]
    }]
}

basically for each ip i have a list of names for telling which usernames have been used on the ip

normaly its not an anon or temp user but for the sake of example i used it of course each ip has its own object so again its another list of objects

also for the the names part if the current user name is in the list of names we are iterating through then when they join the session is added and the id is added hence why its a list of ids and sessions to make the flow smoother and to prevent duplicate entries of certain users as much as possible in the whois (although not completely)

also please refer here for the _ module http://underscorejs.org

if i need to add more info please let me know