Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9139857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:26:30+00:00 2026-06-17T09:26:30+00:00

I’m using node js to create a multi player game website. so every time

  • 0

I’m using node js to create a multi player game website. so every time a socket is connected I add that socket in an array like this

var users = [];
var games = [];
var joinKey = [];
var key = 0;
var usersKey = 101;
var gamesKey = 201;

io.sockets.on('connection', function (socket) {
    socket.on('connect', function(){
        console.log('connect');
        var clientId = usersKey;
        socket.clientId = clientId;
        users[usersKey] = socket;
        usersKey++;
        socket.emit('getClientId', clientId);
        console.log(socket.clientId + ' connected');
        console.log('Total users: ' + users.length);
    });

here total users shows 102 as user key is 101 and it automatically tales blank values from 0 to 100. this is not normal i guess. also if I retrieve the socket from other array using index, it is undefined if the index is string, like ‘g201’

socket.on('createNewGame', function(){
        var game = [];
        var clientId = socket.clientId;
        game[clientId] = socket;
        console.log(game);
        var gameId = 'g' + gamesKey;
        gamesKey++;
        games[gameId] = game;
        console.log('Total Games: ' + games.length)
        socket.gameId = gameId;

        var publicKey = ++key;
        joinKey[publicKey] = gameId;

        socket.emit('gameCreated', publicKey);
    });

I’m not getting how to insert and fetch if this is the behavior of array here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:26:30+00:00Added an answer on June 17, 2026 at 9:26 am

    To answer your first point:

    An array uses numerical indices running from 0.

    When you manually place a value in at 101 it will fill the rest of the values in since it has to run from 0, these will be padded with undefined. (as per Andrew Barretts comment)

    To answer your second point:

    If you need to have an “associate array” or at least what people refer to as an associative array then you should use objects.

    game= new Object();
    game["g1"]=...
    game["g25"]=...
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am confused How to use looping for Json response Array in another Array.
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.