一维的二维JavaScript数组问题被忽略

一维的二维JavaScript数组问题被忽略

问题描述:

简而言之,我正在根据当前月份和日期填充一个数组.我不会在这里重复代码以获取当前的月份和日期,因为它可以正常工作.它会适当地返回变量"month"和"day".我的阵列列表中包含一年中每一天的项目.数组以...

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]);

它将忽略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没有多维数组.此外,其数组的索引从0开始,而不是从1开始.并且第一条语句中的new不属于该位置.

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.

所以代替这个:

new var content = [[]];

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

您可以使用此:

var content = [ [], [] ];

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

或者类似的内容.

您应该阅读JavaScript数组语法.听起来您在尝试某些事情时并没有真正知道该语言中什么是有效的.像这样进行实验是件好事,但是如果您知道什么样的实验可能会成功,那就更好了.

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';

有效的JavaScript.它有什么作用?好的,JavaScript有一个逗号运算符,它可以计算逗号两侧的表达式并返回右侧的表达式值.所以这个表达式:

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

的值为2.换句话说,array[1,2]忽略1,实际上与array[2]相同,即它返回数组的第三个元素.

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

您可以像这样在JavaScript控制台中进行测试:

You can test that in the JavaScript console like this:

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

将打印:

[30, 40]

更新:

以下是您在评论中链接到的代码的摘录:

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 = [ [], [] ];

这将创建一个包含两个元素的数组,其中每个元素本身都是一个空数组.由于JavaScript从0开始索引数组,因此这两个元素是content[0]content[1].没有content[2]content[3]等.这些不存在.

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.

然后执行以下语句:

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

现在content包含:

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

不是您想要的,是吗?

当您得到此声明时:

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

您将收到一个JavaScript错误:

You will get a JavaScript error:

TypeError: Cannot set property '1' of undefined

那是因为如上所述,content[2]不存在.

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

就像我说的那样,您需要研究一些基本的JavaScript概念,例如数组工作原理的细节.有很多很好的教程和参考资料.

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.

还学习了如何使用JavaScript调试器,因此您可以看到上面的错误,并可以交互地尝试代码.

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",
        ...
    ],
    ...
];

然后content[0][0]"January First"content[1][1]"February Second",依此类推.

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

如果要在几个月和几天中使用基于1的索引,可以执行以下操作:

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",
        ...
    ],
    ...
];

现在content[1][1]"January First"content[2][2]"February Second",依此类推.

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