Forum Moderators: open
var string = "\"string variable\""
var arr = new Array()
arr[0]=[string]
arr[1]=["array value 1"]
arr[2]=["array value 2"]
arr[3]=["array value 3"]
I also tried it this way, without success:
var string = "[\"string variable\"]"
var arr = new Array()
arr[0]=string
arr[1]=["array value 1"]
arr[2]=["array value 2"]
arr[3]=["array value 3"]
Each value of my array has a text value, in [brackets] and "quotes" as I indicated. I formatted the variable I want to add the same way, but the script returns "undefined" for arr[0].
(FYI, the reason I'm doing this is that, while the rest of my array values are set, this one is calculated/defined earlier in the script, and can be one of two strings depending on the user's system settings.)
if (screen.width<1024) var string = "lo-res text"
else var string = "hi-res text"
var arr = new Array()
arr[0]=[string]
arr[1]=["array value 1"]
arr[2]=["array value 2"]
arr[3]=["array value 3"]
In the above example, arr[0] should =
["lo-res text"]
or
["hi-res text"]
based on the user's screen res.
Each value of my array has a text value, in [brackets] and "quotes" as I indicated.
Hmm. There is risk of further confusion here.
Are the brackets for our benefit, or do they actually appear in the code?
(Just in case you don't know..) Square brackets can be used in array initialisers - basically a shorthand form of array creation.
These 3 bits of code all do the same thing..
(1)-----------------------------
[blue]var arr = new Array();
arr[0] = "A";
arr[1] = "B";
arr[2] = "C";[/blue]
(2)-----------------------------
[blue]var arr = new Array("A","B","C");[/blue]
(3)-----------------------------
[blue]var arr = ["A","B","C"];[/blue]
-------------------------------- The code you have posted looks like a 2-dimensional array is being created, using a mixture of syntax...
var arr = new Array()
arr[0]=[string]
arr[1]=["array value 1"]
arr[2]=["array value 2"]
arr[3]=["array value 3"]
See what I mean?
Setting the value of an array element is very straightforward. I suspect the problem is somewhere else. Possibly need to see more.
var arr = new Array()
arr[0]=["array value 0"]
arr[1]=["array value 1"]
arr[2]=["array value 2"]
arr[3]=["array value 3"]
I want to replace the first value -- ["array value 0"] -- with var string, in a format that will be read properly by the array.
var string = "hello";
//
var arr = new Array();
arr[0]=[string];
arr[1]=["array value 1"];
arr[2]=["array value 2"];
arr[3]=["array value 3"]; In this case:
alert(arr[0][0]) [green]// --> hello[/green] To add the value after the array has been created:
arr[0] = [string]; Or, if the sub-array, arr[0] has already been created:
arr[0][0] = string; Why are you using nested arrays? (if you don't mind me asking)
Review my third post to see what I'm trying to accomplish; that may clarify things. I'm just trying to substitute a script-calculated array value for a pre-set one.
Regarding the code in your last post, when I test it by itself exactly as given, with the alert message, it works fine. But when I use it in my page, I still get 'undefined' for arr[0].
Two questions:
First, regarding quotes, as I indicated, my array values are defined with quotes -- i.e. arr[0]=["quoted text"] . And when I define var string = "text"; I also use quotes. So I've formatted the string definition this way:
var string = "\"text\"";
Is that correct? (As I said, when I test it with the alert, it correctly reads "text" with the quotes included.)
The second point may be causing the glitch. In my effort to simplify the code for posting here, I omitted the fact that each array value actually has two separate strings, so it really looks like this:
var arr = new Array()
arr[0]=["text 0a", "text 0b"]
arr[1]=["text 1a", "text 1b"]
arr[2]=["text 2a", "text 2b"]
arr[3]=["text 3a", "text 3b"]
Accordingly, I defined my string like this (including the if/else conditional):
if (screen.width<1024) {
var string = "\"lo-res text a\", \"lo-res text b\"";
} else
var string = "\"hi-res text a\", \"hi-res text b\"";
When I test it with your alert, it appears correct to me (for hi-res monitor):
"hi-res text a", "hi-res text b" (quotes included)
I didn't think I was nesting arrays -- one of us isn't clear about what's going on (probably me!)
Yes arr[0] to [3] are all arrays themselves.
var string = "\"text\"";Is that correct?
Yes. You may find it easier to use single quotes for the actual syntax-meaningful quotes. This saves mucking around with backslashes:
var string = '[blue]"text"[/blue]'; However, I'm still confused about this quoting issue, because, although the inserted string will be quoted, the rest of the strings (as defined in the array) are not quoted. The quotes there are just syntactical.
Accordingly, I defined my string like this (including the if/else conditional):
This is where you are falling off the path, I think.
When I test it with your alert, it appears correct to me Yes, but arr[0] is a string, whilst arr[1]-[3] are arrays (of strings).
The outer array contains 2-element arrays of strings, yet you are creating a single string to replace the first sub-array with. This might not actually matter, depending on how you are treating the array afterwards. For consistency, you could do this (in shorthand):
var screenStrings = screen.width<1024
? ['"lo-res text a"','"lo-res text b"']
: ['"hi-res text a"','"hi-res text b"'];
alert(arr[0]) will still give the same output (all other things being equal), but that is because the default toString method for an array will produce all the array members (0th - last) converted to strings, and separated by commas.