带有字符串键的动态json对象

时间:2016-07-21 05:35:52

标签: javascript json object

我添加了一个带有javascript的动态json对象。代码如下:

if (!beadz[color.title])
    beadz.push({
    (color.title): {
        bead.title: {
            "inventoryQuantity": bead.inventory_quantity,
            "currentQuantity": 1
        }
    }
});

但是代码无效并且出现语法错误。我错过了什么? 善意的帮助。

1 个答案:

答案 0 :(得分:1)

如果您使用的是ES6

var players = [];
io.on('connection', function(socket){


    //player is given 'P' with random number when connection is made to represent username
  socket.name = "P" + Math.floor(Math.random() * (20000));
  // Here may be you can assign the position also where the user is sitting along with the user name. 
  //players will be an array of object which holds username and their position
  //So that in client you can decide where the user will sit.
  players.push(socket.name);
  io.emit('playerinfo', {'players':players, 'name':socket.name);
  socket.on('move', function (data) {
     socket.emit('move', data);
  });
}