简体   繁体   English

Javascript:如何将数组值添加到对象属性

[英]Javascript: How to add array values to object property

Following code is for socket.io server in node.js 以下代码适用于node.js中的socket.io服务器

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var clients=[];
var gamename={};

io.on('connection', function(socket){
  socket.on('game', function(data){
    gamename[data.gamename]=data.number; //problem is here 
  });
});

gamename=name of the game; gamename =游戏名称; number= user id; number =用户ID;

  1. there can be more number of games; 可以有更多的游戏;
  2. there can be more number of users per game 每场比赛可以有更多的用户

and I am emitting game event in the client side with some data (includeing gamename, and number)whenever a connection is established with the server. 每当与服务器建立连接时,我在客户端发出一些数据(包括游戏名称和号码)的游戏事件。 So whenever a new client connects to the server the game event is triggered in the server. 因此,每当新客户端连接到服务器时,在服务器中触发游戏事件。 In the game event in the server side I want to push "number" to the object property gamename(variable). 在服务器端的游戏事件中,我想将“数字”推送到对象属性gamename(变量)。

example: 例:

var games={};

whenever there is a connection for example for the game poker with user id 34 I want to do var gamename='poker'; 每当有用户ID为34的游戏扑克连接时,我想做var gamename ='poker';

 gamename[gamename] -> I want this automatically created as array, or   anything, but I want to push user id's.

the resulting objecting should be. 产生的反对应该是。

games={'poker' : [34]};

If I one more user connects for poker with user id 45, 如果我再一个用户连接用户ID为45的扑克,

games={'poker' : [34, 45]};

and If aa user connects for game cricket with user 67 如果用户用户67连接游戏板球

games={'poker' : [34, 45],'cricket' : [67]};

Initial object 初始对象

games={};

after some connections(there can be n number of connections) 一些连接后(可以有n个连接)

games={'poker' : [34, 45],'cricket' : [67]};

If my question is not clear I will explain it in detail if anybody asks. 如果我的问题不清楚,我会详细解释,如果有人问。 Thanks in advance 提前致谢

Like this: 像这样:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var clients=[];
var gamename={};

io.on('connection', function(socket){
  socket.on('game', function(data){
    gamename[data.gamename] = gamename[data.gamename] || [];
    gamename[data.gamename].push( data.number ); //no more problem 
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 object 数组 Javascript 中添加随机值? - How to add random values in an object array Javascript? 如何在javascript中添加数组对象值 - how to add array object values in javascript 如何使用 javascript 中的数组值更改数组 object 中的属性值 - How to change property value in array object with array values in javascript 如何将数组 Object 中的属性值替换为 Javascript 中另一个数组 Object 中的属性值 - How to Replace values of a property in an Array Object with values of a property in another Array Object in Javascript 如何将对象数组中单个属性的值相加? - How to add up the values of a single property in an object array? 如何使用JavaScript在嵌套对象数组中按属性分配值 - How to assign values by property in nested object array using javascript 如何在JavaScript中将数组元素指定为对象中的属性值? - How to specify array elements as property values in an object in JavaScript? 如何将 JavaScript object 的属性值提取到数组中? - How might I extract the property values of a JavaScript object into an array? 如何检查两个数组中的属性值 object 值 javascript - how to check property value in two array of object values javascript 如何使用javascript的变量名在数组内添加对象属性? - How to add an object property inside an array using a variable name for javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM