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 9208015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:31:13+00:00 2026-06-18T00:31:13+00:00

i want to create an array like this [ {sessionId: ‘12345’, nickname: ‘timmay!’, socketIds:

  • 0

i want to create an array like this

[
    {sessionId: '12345', nickname: 'timmay!', socketIds: [1, 2, 3]},
    {sessionId: '23456', nickname: 'pete', socketIds: [4, 5, 6]}
]

i defined an array like this

var  myarray = []

and now i want to create this structure like above example

if you noticed we have an array inside our object socketIds: [4, 5, 6]
how should i create an array which contain many objects and how should i call them?

for above example if we name this myarray how should i call socketIds 1 or timme!?

  • 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-18T00:31:14+00:00Added an answer on June 18, 2026 at 12:31 am

    You have two questions:

    how should i create an array which contain many objects?

    var myarray = [];
    myarray[0] = {};//make an empty object
    myarray[0].sessionId = '12345';//in JS assigning something to a property of an object creates/updates it
    myarray[0].nickname = 'timmay!';
    myarray[0].socketIds = [];//create an empty array
    myarray[0].socketIds[0] = 1;//assign the first element of array called socketIds in the first object of the array called myarray
    myarray[0].socketIds[1] = 2;
    myarray[0].socketIds[2] = 3;
    
    myarray[1] = {};
    myarray[1].sessionId = '12345';
    myarray[1].nickname = 'timmay!';
    myarray[1].socketIds = [ 4, 5, 6];
    

    how should i call them for example for above example if we name this myarray how should i call socketIds 1 or timme!?

    alert( myarray[0].socketIds[0] );
    alert( myarray[0].nickname );
    

    Edit1:

    Usually you will use a for() loop to deal with arrays but if you want to squeeze the maximum performance from your Javascript engine you may consider saving the array references instead of referencing them every time. Like this:

    var myarray = [];
    var tmpObj = myarray[0] = {};//make an empty object
    tmpObj.sessionId = '12345';//in JS assigning something to a property of an object creates/updates it
    tmpObj.nickname = 'timmay!';
    var tmpArr = tmpObj.socketIds = [];//create an empty array
    tmpArr[0] = 1;//assign the first element of array called socketIds in the first object of the array called myarray
    tmpArr[1] = 2;
    tmpArr[2] = 3;
    

    but I’m pretty sure modern Javascript engines take care of these repeated references and the ultimate code will be as fast as without saving the references.

    Edit2:

    Of course obviously what you have written in your own question is a valid syntax for initializing the array too (object and array literals):

    var myarray = [
        {
            sessionId: '12345', 
            nickname: 'timmay!', 
            socketIds: [1, 2, 3]
        },
        {
            sessionId: '23456',
            nickname: 'pete',
            socketIds: [4, 5, 6]
        }
    ];
    

    The problem with this syntax is that you have to hard-code the values and that is not the most popular way to create complex data structures in Javascript. Anyway it will create the same object as the first solution I mentioned in the beginning of this answer. So you can still access the parts like this:

    alert( myarray[0].socketIds[0] );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine that I want to create an array from another array like this: $array
I want to create an array like this [1, 1, 1, 1, 1] In
i want to create array with row like this : dateMetric : [{date :
I want to create a string-array inside another string-array like this: <string-array name=names> <string-array
I want to create list of years in array like this: array ( '2011'
I want to create a movie from an array of UIImages. I tried this
I want to create an array from a string that contains brackets like {!
I want to create a Calendar View Like this example: http://bl.ocks.org/4063318 : Actually i
I want to create an array of arrays in PowerShell. $x = @( @(1,2,3),
I want to create an array holding a function f(x,y,z) . If it were

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.