1

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!?

2
  • 2
    I don't get your question to be honest.
    – Sirko
    Jan 28, 2013 at 14:17
  • 1
    Please clarify what you want to do, and what data you want to access. A clear, detailed explanation is best, but you can also use pseudocode to clarify your question, if necessary.
    – apsillers
    Jan 28, 2013 at 14:18

5 Answers 5

3

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] );
11
  • tnx best answer ill accept in 7 minutes i am just so dumb to ask such question :)
    – HiDd3N
    Jan 28, 2013 at 14:23
  • Constantly de-referencing with myArray[i] is a no-no
    – Dexygen
    Jan 28, 2013 at 14:23
  • Don't except this answer, it's chock full of bad Javascript practices
    – Dexygen
    Jan 28, 2013 at 14:24
  • @GeorgeJempty george what you suggest?
    – HiDd3N
    Jan 28, 2013 at 14:25
  • 1
    @AlexStack yes ofcourse its just an example and tnx Alex.realy great example.you even clarify what i wanted.
    – HiDd3N
    Jan 28, 2013 at 14:28
2

This is how you need to call

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

    document.write(arr[0].nickname+"<br>");
    document.write(arr[0].socketIds[0]);
1

this data structure is similar to python dictionary type. try this Dictionary equivalent data structure?

1

Not sure if your question is really this simple, but the code you showed is already creating an array of objects, using literal notation. So just assign that to a variable:

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

To access the elements you mentioned:

console.log(myarray[0].socketIds[0]); // 1
console.log(myarray[0].nickname);     // "timmay!"

If you later want to add another object:

var newObject = {sessionId: '34567', nickname: 'john', socketIds: [7, 8, 9]};
myarray.push(newObject);
1

Initialize your variables as follows:

var myArray = [{}, {}],
    socket1 = myArray[0],
    socket2 = myArray[1];

And now do your assignments, to socket1 and socket2 respectively. This may not answer your question completely, but it shows the accepted way for doing these sorts of assignments, if you have a finite and small number of sockets.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.