inherit
125435
0
Jun 1, 2013 7:46:21 GMT -8
Lugubrious
I love arrays...
790
May 2008
lugubrious
|
Post by Lugubrious on Mar 7, 2010 17:29:48 GMT -8
I'm trying to make a two-dimensional array, if you get what I mean. Basically...
var opp=new Array(); opp[0]("love","hate"); opp[1]("happy","sad");
And I would like opp[0,1] be hate, etc. Is that possible? I know it is in some languages, but the code is giving me a hard time so it would be nice to know it that is possible. If it is impossible, how can I get around this?
|
|
inherit
125499
0
Nov 8, 2011 4:03:57 GMT -8
moneyman18
:-
952
June 2008
moneyman18
|
Post by moneyman18 on Mar 7, 2010 17:48:21 GMT -8
The correct way to do something like that would be like this:
var opp=new Array(); opp[0] = ["love","hate"]; opp[1] = ["happy","sad"];
To access hate of the array would be opp[0][1] not opp[0,1].
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Mar 7, 2010 17:48:51 GMT -8
There's several ways to write it, but this will do. And you don't use commas to separate the indices.
var opp = [ ["love", "hate"], ["happy", "sad"] ];
opp[0][1] == "hate"
|
|
inherit
125435
0
Jun 1, 2013 7:46:21 GMT -8
Lugubrious
I love arrays...
790
May 2008
lugubrious
|
Post by Lugubrious on Mar 7, 2010 17:59:38 GMT -8
Thanks so much! Yall are the best.
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on Mar 9, 2010 20:18:59 GMT -8
While what Jordan posted is the new standard for arrays, hopefully this will help you understand the declaration of arrays.
var opp=new Array(); opp[0]("love","hate"); opp[1]("happy","sad");
should be
var opp = new Array(); opp[0] = new Array("love","hate"); opp[1] = new Array("happy","sad");
[] is synonymous with new Array() var opp = []; opp[0] = ["love", "hate"]; opp[1] = ["happy", "sad"]; is saying the same thing as the above example, and is the current JS standard (so use [] instead of new Array()).
What I'm getting to is: You can declare a value of an array (e.g. opp[0]) the same way you'd declare the array itself, or any other variable. some_variable = []; some_array[0] = [];
some_variable = "string"; some_array[1] = "string";
And I'm not sure if other languages support this (I know the outdated C++ I was learning ages ago didn't), but you can also do: var opp = [ ["an", "array", "for", "the", "first", "value"], "a string for the second", 124959, ["back", "to", "an", "array"] ];
Each value can be anything, including an array, of any length.
|
|