二维数组javascipt的问题随着第一个维度忽略 [英] Two Dimensional JavaScipt Array Issue With First Dimension Ignored

查看:118
本文介绍了二维数组javascipt的问题随着第一个维度忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简言之,我填充基于当前月份和日期的阵列。我不会重复code在这里获得当前的月份和日期作为其正常工作。它返回变量月和天恰如其分。我的数组列表有一年的每一天的项目。该阵列开始...

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没有多维数组。此外,它的数组索引从0开始,而不是1以及在你的第一次发言不属于那里。

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

您可以使用此:

var content = [ [], [] ];

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

或沿着类似的路线的东西。

Or something along similar lines.

您应该对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有一个逗号运营商,这对评估逗号两侧的前pressions并返回右边的前pression的价值。所以这前pression:

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的值。换句话说,数组[1,2] 忽略 1 ,是有效地同数组[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]

更新:

下面是你在评论链接到code的摘录:

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.";
// ...

因此​​,让我们来谈谈这是什么code确实。首先,该行:

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

var content = [ [], [] ];

这产生两个元素,其中每一元素是本身空数组的数组。因为从0 JavaScript的指标阵列,这两个元素是内容[0] 内容[1] 。没有内容[2] 内容[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.";

现在内容包含:

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

您想不了的事,是吧?

当你得到这个语句:

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

您会得到一个JavaScript错误:

You will get a JavaScript error:

TypeError: Cannot set property '1' of undefined

这是因为如上所述,内容[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调试器,所以你可以看到类似上面的错误,这样你就可以用$ C $实验C交互。

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

然后内容[0] [0] 一首内容[1] [1] 二月二

如果您希望使用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",
        ...
    ],
    ...
];

现在内容[1] [1] 一首内容[1] [2] 二月二

这篇关于二维数组javascipt的问题随着第一个维度忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆