有没有更好的方法将用户添加到对象/数组

时间:2017-01-06 21:03:00

标签: javascript node.js socket.io

我正在创建一个连接到我的node.js服务器的android应用程序,该服务器正在使用socket.io。

现在我没有任何问题,但我觉得有更好的方法可以做到这一点。以下代码根据用户的位置国家/州/城市将用户添加到userPool变量。

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'] };

});

2 个答案:

答案 0 :(得分:2)

这样的事情:

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 :(得分:1)

幸运的是,有些图书馆会为您处理这项繁重的工作。如果我正确地遵循你的逻辑:

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