var tags = new Array();
var tags[4]= new Array();
tags[4].push("Hello");
Somehow this doesnt work, console Says on line two that there's an unexpected Token... Can you help me somehow? Its an Array inside an Array. I simplified the code, because the rest is right.
Thx
-
possible duplicate of push() a two-dimensional array with JavaScriptozahorulia– ozahorulia2014年02月12日 13:51:18 +00:00Commented Feb 12, 2014 at 13:51
-
@Hast - I really don't see how that is a duplicate...Lix– Lix2014年02月12日 13:52:20 +00:00Commented Feb 12, 2014 at 13:52
-
@Lix It mentions two-dimensional arrays and pushing, it must be a duplicate, don't you see? :pNiet the Dark Absol– Niet the Dark Absol2014年02月12日 13:58:08 +00:00Commented Feb 12, 2014 at 13:58
-
@NiettheDarkAbsol - gosh I hope you were being sarcastic :PLix– Lix2014年02月12日 13:59:08 +00:00Commented Feb 12, 2014 at 13:59
6 Answers 6
var tags[4] is incorrect. Just tags[4] is needed.
1 Comment
It's a simple mistake to make. Just remove var from line 2...
var tags = new Array();
tags[4]= new Array();
tags[4].push("Hello");
tags[4] is already available by declaring tags on line 1.
Comments
Remove the var before tags[4]. tags is the variable, tags[4] is a property of the object referenced by that variable, not another variable.
var tags = new Array();
tags[4]= new Array();
tags[4].push("Hello");
Comments
var tags[4] // is incorrect.
// use this
tags[4]= new Array();
tags[4].push("Hello");
var keyword creates a variable so old value is lost .
Comments
The array tags has already been initialized, so you don't need var on the second line. Remove it and the code works as expected.
Comments
Remove var before tag[4]
Try this instead.
var tags = new Array();
tags[4]= new Array();
tags[4].push("Hello");