2017-01-06 6 views
-1

Ich erstelle eine Android-App, die eine Verbindung zu meinem node.js-Server, der socket.io verwendet.Gibt es eine bessere Möglichkeit, Benutzer zu Objekt/Array hinzufügen

Jetzt habe ich keine Probleme, aber ich habe das Gefühl, es gibt einen besseren Weg, dies zu tun. Der folgende Code fügt den Benutzer der userPool Variable basierend auf dem Standort des Benutzers Land/Bundesland/Stadt hinzu.

var userPool = {}; 

// Adds the user to the pool object 
socket.on('add or update user to pool', function(data) { 
    if (userPool.hasOwnProperty(data['country'])) { 
    if (userPool[data['country']].hasOwnProperty(data['state'])) { 
     if (userPool[data['country']][data['state']].hasOwnProperty(data['city'])) { 
     if (userPool[data['country']][data['state']][data['city']].hasOwnProperty(data['user_type'])) { 
      userPool[data['country']][data['state']][data['city']][data['user_type']][socket.id] = data; 
     } 
     else { 
      userPool[data['country']][data['state']][data['city']][data['user_type']] = {}; 
      userPool[data['country']][data['state']][data['city']][data['user_type']][socket.id] = data; 
     } 
     } 
     else { 
     userPool[data['country']][data['state']][data['city']] = {}; 
     userPool[data['country']][data['state']][data['city']][data['user_type']] = {}; 
     userPool[data['country']][data['state']][data['city']][data['user_type']][socket.id] = data; 
     } 
    } 
    else { 
     userPool[data['country']][data['state']] = {}; 
     userPool[data['country']][data['state']][data['city']] = {}; 
     userPool[data['country']][data['state']][data['city']][data['user_type']] = {}; 
     userPool[data['country']][data['state']][data['city']][data['user_type']][socket.id] = data; 
    } 
    } 
    else { 
    userPool[data['country']] = {}; 
    userPool[data['country']][data['state']] = {}; 
    userPool[data['country']][data['state']][data['city']] = {}; 
    userPool[data['country']][data['state']][data['city']][data['user_type']] = {}; 
    userPool[data['country']][data['state']][data['city']][data['user_type']][socket.id] = data; 
    } 

    if (!socket.hasOwnProperty('userInfo')) 
    socket['userInfo'] = { 'country': data['country'], 'state': data['state'], 'city': data['city'], 'user_type': data['user_type'] }; 

}); 
+0

Ein wenig schwer zu folgen, aber ich bin viel Code Wiederholung zu bemerken. So viel du kannst, versuche dich nicht zu wiederholen. – deweyredman

+1

Sollte dies nicht auf [CodeReview] (http://codereview.stackexchange.com/) gefragt werden? – Xufox

Antwort

2

was ist so etwas wie dieses:

let userPool = {}; 

socket.on('add or update user to pool', addUserToPool); 

function addUserToPool (data) { 
    let country  = data['country'] 
     , state  = data['state'] 
     , city  = data['city'] 
     , user_type = data['user_type']; 

    userPool[country] = userPool[country] || {}; 
    userPool[country][city] = userPool[country][city] || {}; 
    userPool[country][city][user_type] = userPool[country][city][user_type] || {}; 

    let userTypes = userPool[country][city][user_type]; 

    userTypes[socket.id] = data; 
} 
1

Zum Glück gibt es Bibliotheken, die sich um diese Arbeit kümmern. Wenn ich nach Ihrer Logik richtig:

import extend from 'deep-extend'; 

socket.on('add or update user to pool', (data) => { 
    userPool = extend(userPool, { 
    [data.country]: { 
     [data.state]: { 
     [data.city]: { 
      [data.user_type]: { 
      [socket.id]: data 
      } 
     } 
     } 
    } 
    }); 
}); 
Verwandte Themen