views:

132

answers:

4

This is probably a fairly simple way to do it, but I ask since I did not find it on google.

What I want is to add an array under another.

var array = [];

array[0]["username"] = "Eric";
array[0]["album"] = "1";

Something like this, I get no errors in Firebug, it does not work. So my question is, how does one do this in javascript?

+7  A: 
var a= [
 { username: 'eric', album: '1'},
 { username: 'bill', album: '3'}
];
pygorex1
thx a lot! men vist du da ønsker å legge til flere i arrayet i ettertid da? I mitt tilfellet er det en loop
sv88erik
I'm so sorry, was so happy for your reply, I wrote it in my language. What I would say is: thx a lot! but shown when you want to add more in the array later then? In my case it is a loop
sv88erik
var a = [];a.push({username: "foo", album: "3"})
roryf
+1  A: 

try something like this

array[0] = new Array(2);
array[1] = new Array(5);

More about 2D array here

pramodc84
A: 

You should get an error in Firebug saying that array[0] is undefined (at least, I do).

Using string as keys is only possible with hash tables (a.k.a. objects) in Javascript.

Try this:

var array = [];
array[0] = {};
array[0]["username"] = "Eric";
array[0]["album"] = "1";

or

var array = [];
array[0] = {};
array[0].username = "Eric";
array[0].album = "1";

or simpler

var array = [];
array[0] = { username: "Eric", album: "1" };

or event simpler

var array = [{ username: "Eric", album: "1" }];
Vincent Robert
A: 

Decompose.

var foo = new Array();
foo[0] = new Array();
foo[0]['username'] = 'Eric';
foo[0]['album'] = '1';
Boris Guéry