简体   繁体   English

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

[英]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. 它会适当地返回变量“ month”和“ day”。 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. 它会忽略month变量,并转到该特定日期的最后一个可用的day变量。

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. JavaScript没有多维数组。 Also, its arrays are indexed starting at 0, not 1. And the new in your first statement doesn't belong there. 同样,其数组的索引从0开始,而不是从1开始。而且,第一条语句中的new数组不属于该数组。

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. 您应该阅读JavaScript数组语法。 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. 有效的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. 好的,JavaScript有一个逗号运算符,它可以计算逗号两侧的表达式并返回右侧的表达式值。 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. 的值为2。换句话说, array[1,2]忽略1并实际上与array[2]相同,即它返回数组的第三个元素。

You can test that in the JavaScript console like this: 您可以像这样在JavaScript控制台中进行测试:

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] . 由于JavaScript从0开始索引数组,因此这两个元素分别是content[0]content[1] There is no content[2] , content[3] , etc. Those do not exist. 没有content[2]content[3]等。这些都不存在。

Then you execute this statement: 然后执行以下语句:

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

Now content contains: 现在content包含:

[ [], [ 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: 您将收到一个JavaScript错误:

TypeError: Cannot set property '1' of undefined

That's because as mentioned above, content[2] does not exist. 这是因为如上所述, content[2]不存在。

Look, as I said, you need to study up on basic JavaScript concepts like the details of how arrays work. 就像我说的,您需要研究一些基本的JavaScript概念,例如数组工作原理的细节。 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. 另请学习如何使用JavaScript调试器,以便您可以看到上述错误,并可以交互地进行代码实验。

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. 然后content[0][0]"January First"content[1][1]"February Second" "January First""February Second"

If you want to use 1-based indexing for the months and days, you could do this: 如果要在月和日中使用基于1的索引,可以执行以下操作:

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. 现在content[1][1]"January First"content[2][2]"February Second" ,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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