2

I would like to create an object with a similar data structure if possible. Must I create a new object for every player? Could somebody tell me how?

players
players.name='John'

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

edit1

Sorry I know this is the basic. I just ask one more question an them i will try learn better javascript. I need to pass data stored in "event" to object."event".id (to be like players.John.id instead players.event.id) Sorry for my bad english.

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

edit2

Got it working.

// 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
  • Depending on visibility requirements you might want to have the first line as var players;, otherwise you have a valid JS code that creates a single object and initializes its properties. Commented Jun 17, 2014 at 18:48
  • Possible duplicate? stackoverflow.com/questions/1114024/… Commented Jun 17, 2014 at 18:54

2 Answers 2

5

You're looking for a players object, with individual players referenced by name. So:

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

You can also access "John" as players.John, but that gets tricky if any of the names contain spaces, etc.

Similarly, the player attributes can be accessed either via:

players.John['age'] = 13;   

or

players.John.age = 13;
Sign up to request clarification or add additional context in comments.

3 Comments

Just remember that keys have to be unique. This method only works for a short lucky while until you have two players named John. It doesn't make sense to assign players as an object like this. Use a plan old array, and have a name property of each.
Unless you want to do something crazy like "look up a player by name". But yes, ensuring unique names matters.
Looking up a player by name is quite easy by looping through. If that's inefficient, then a proper search tree can be implemented or let a database engine deal with it for you.
3
var name = "John";
var players = {};
players[name] = {};
players[name].age = '12';
players[name].address = "address";
players[name].telnumber = "tel";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.