gpt4 book ai didi

Javascript 使用这样的数据结构创建对象

转载 作者:搜寻专家 更新时间:2023-11-01 00:13:13 25 4
gpt4 key购买 nike

如果可能的话,我想创建一个具有类似数据结构的对象。我必须为每个玩家创建一个新对象吗?谁能告诉我怎么做?

players
players.name='John'

players.John.age='12'
players.John.adress='London ..'
players.John.telnumber='09876587655'

编辑1

抱歉,我知道这是基本的。我只是再问一个问题,我会尝试学习更好的 javascript。我需要将存储在“事件”中的数据传递给对象。“事件”。id(就像players.John.id而不是players.event.id)抱歉我的英语不好。

// in app.js
var fs = require('fs');
var socketio = require('socket.io');
Tail = require('tail').Tail;
var express = require('express');
var http = require('http');
var colors = require('colors');
var app = express()
, server = require('http').createServer(app)
, io = socketio.listen(server); // socket needs to listen on http server

server.listen(9099);



app.use(express.static(__dirname + '/public'));

var port = process.env.PORT || 3000;

app.listen(port, function() {
console.log('\r\n');
console.log("Express listening on port " + port +'.'.green);
});

// Routing
//app.use(express.static(__dirname));

// usernames which are currently connected to the chat
//var players = [];
var players = {};


io.sockets.on('connection', function(socket) {
// do all of your socket work in here
console.log('\r\n');
console.log("Connection".green);

var sessionid = socket.id;
console.log(sessionid);

// Success! Now listen to messages to be received
socket.on('message',function(event){
console.log('Received message:',event);
});

socket.on('add user',function(event){
console.log('New User:',event);
// we store the username in the socket session for this client

socket.username = event;
// add the client's username to the global list
players.event = {};
players.event.id = sessionid;
//players.John.foo = "yeah"
//players.John.name = "John"
console.log(players);

socket.emit('login', {});
// echo globally (all clients) that a person has connected

socket.broadcast.emit('user joined', {
username: socket.username
});
});

//socket.emit('start', 'newround');
});

编辑2

成功了。

// in app.js
var fs = require('fs');
var socketio = require('socket.io');
Tail = require('tail').Tail;
var express = require('express');
var http = require('http');
var colors = require('colors');
var app = express()
, server = require('http').createServer(app)
, io = socketio.listen(server); // socket needs to listen on http server

server.listen(9099);

app.use(express.static(__dirname + '/public'));

var port = process.env.PORT || 3000;

app.listen(port, function() {
console.log('\r\n');
console.log("Express listening on port " + port +'.'.green);
});

// Routing
//app.use(express.static(__dirname));

// usernames which are currently connected to the chat
//var players = [];
var players = {};

io.sockets.on('connection', function(socket) {
// do all of your socket work in here
console.log('\r\n');
console.log("Connection".green);
var sessionid = socket.id;
console.log(sessionid);

// Success! Now listen to messages to be received
socket.on('message',function(event){
console.log('Received message:',event);
});

socket.on('add user',function(event){
console.log('New User:',event);
// we store the username in the socket session for this client

socket.username = event;
// add the client's username to the global list
players[event] = {};
players[event].id = sessionid;
//players.John.foo = "yeah"
//players.John.name = "John"
console.log(players);

socket.emit('login', {});

// echo globally (all clients) that a person has connected
socket.broadcast.emit('user joined', {
username: socket.username
});
});

//socket.emit('start', 'newround');
});

最佳答案

您正在寻找一个 players 对象,其中包含按名称引用的各个玩家。所以:

var players = {};
players['John'] = {
'age' = 12,
'address' = 'London...',
'telnumber' = '09876587655'
};

您也可以作为 players.John 访问“John”,但如果任何名称包含空格等,这会变得棘手。

同样,可以通过以下方式访问播放器属性:

players.John['age'] = 13;   

players.John.age = 13;

关于Javascript 使用这样的数据结构创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24271136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com