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!?
You have two questions:
how should i create an array which contain many objects?
how should i call them for example for above example if we name this myarray how should i call socketIds 1 or timme!?
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:
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):
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: