【问题标题】:Is there a better way to add user to object / array有没有更好的方法将用户添加到对象/数组
【发布时间】:2017-01-06 21:02:59
【问题描述】:

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

现在我没有任何问题,但我觉得有更好的方法来做到这一点。下面的代码根据用户所在的国家/地区/州/城市将用户添加到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'] };

});

【问题讨论】:

  • 有点难以理解,但我注意到很多代码重复。尽量不要重复自己。
  • 不应该在CodeReview上问这个吗?

标签: javascript node.js socket.io


【解决方案1】:

这样的事情怎么样:

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

【讨论】:

    【解决方案2】:

    幸运的是,有一些库可以为您处理这项繁重的工作。如果我正确地遵循了您的逻辑:

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

    【讨论】:

      猜你喜欢
      • 2021-07-12
      • 2023-02-05
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      相关资源
      最近更新 更多