简体   繁体   中英

Two Dimensional JavaScript Array Issue With First Dimension Ignored

Briefly, I'm populating an array based on the current month and date. I won't duplicate the code to gain the current month and date here as it is working properly. It returns the variables 'month' and 'day' appropriately. My array list has an item for each day of the year. The array begins with ...

new var content = [[]];

Then the array is listed like this... (abridged)

content[1,1]= "something for Jan 1";
content[1,2]= "something for Jan 2:;

The problem is that when I ask for ...

document.write(content[month,day]);

it ignores the month variable and goes to the last available day variable for that particular date.

I suspect I'm trying to do something that is not possible, but I'm hoping that it is just a minor mistake. Thanks for your help.

JavaScript doesn't have multidimensional arrays. Also, its arrays are indexed starting at 0, not 1. And the new in your first statement doesn't belong there.

So instead of this:

new var content = [[]];

content[1,1]= "something for Jan 1";
content[1,2]= "something for Jan 2";

You might use this:

var content = [ [], [] ];

content[1][1] = "something for Jan 1";
content[1][2] = "something for Jan 2";

Or something along similar lines.

You should read up on JavaScript array syntax. It sounds like you're trying things without really knowing what will be valid in the language. It is good to experiment like this, but even better if you have an idea of what kinds of experiments are likely to succeed.

Oddly enough, even though a statement like this doesn't do what you want:

array[ 1, 2 ] = 'foo';

It is valid JavaScript. What does it do? Well, JavaScript has a comma operator, which evaluates the expressions on both sides of the comma and returns the value of the expression on the right. So this expression:

1, 2

has a value of 2. In other words, array[1,2] ignores the 1 and is effectively the same as array[2] , ie it returns the third element of the array.

You can test that in the JavaScript console like this:

var a = [ [10,20], [30,40] ];
console.log( a[1,1] );

That will print:

[30, 40]

Update:

Here's an excerpt of the code you linked to in the comment:

var content = [ [], [] ];

content[1][1]= "placeholder.";
content[1][2]= "placeholder.";
// ...
content[1][30]= "placeholder.";
content[1][31]= "placeholder.";
content[2][1]= "placeholder.";
content[2][2]= "placeholder.";
// ...
content[2][30]= "placeholder.";
content[2][31]= "placeholder.";
content[3][1]= "placeholder.";
content[3][2]= "placeholder.";
// ...

So let's talk about what this code really does. First, this line:

var content = [ [], [] ];

That creates an array of two elements, where each of those elements is itself an empty array. Since JavaScript indexes arrays from 0, these two elements are content[0] and content[1] . There is no content[2] , content[3] , etc. Those do not exist.

Then you execute this statement:

content[1][1]= "placeholder.";

Now content contains:

[ [], [ null, "placeholder." ] ];

Not what you wanted, is it?

And when you get to this statement:

content[2][1]= "placeholder.";

You will get a JavaScript error:

TypeError: Cannot set property '1' of undefined

That's because as mentioned above, content[2] does not exist.

Look, as I said, you need to study up on basic JavaScript concepts like the details of how arrays work. There are plenty of good tutorials and references on this.

Also learn how to use the JavaScript debugger, so you can see errors like the one above and so you can experiment with code interactively.

Now back to what you're trying to do. Are you wanting to set up an array for each month of the year, with something for every day of each month? To initialize the array, I would just use one array literal for the whole thing.

If you don't mind indexing from zero, use:

var content = [
    [
        "January First",
        "January Second",
        ...
    ],
    [
        "February First",
        "February Second",
        ...
    ],
    ...
];

Then content[0][0] is "January First" , content[1][1] is "February Second" , etc.

If you want to use 1-based indexing for the months and days, you could do this:

var content = [
    null,
    [
        null,
        "January First",
        "January Second",
        ...
    ],
    [
        null,
        "February First",
        "February Second",
        ...
    ],
    ...
];

And now content[1][1] is "January First" , content[2][2] is "February Second" , etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM