简体   繁体   English

二维 Javascript 数组未返回“第二维数组”的正确长度

[英]2D Javascript Arrays Not Returning Correct Length of "Second Dimension Arrays"

I'm trying to dynamically create a "2D" array in Javascript, but it's more of an array of arrays, obviously.我正在尝试在 Javascript 中动态创建一个“2D”数组,但显然它更像是一个数组数组。 Each array in the array (which I will here on call a "second dimension array") is dynamically allocated based on a file the user uploads.数组中的每个数组(我在这里称之为“第二维数组”)都是根据用户上传的文件动态分配的。 The file is formatted:文件格式如下:

Integer representing the number of lines in the drawing the file represents.整数,表示文件所代表的绘图中的行数。

Integer representing the number of coordinates which make up a line of that drawing.表示构成该绘图线的坐标数的整数。

The coordinates in that line, each pair separated by a newline.该行中的坐标,每对由换行符分隔。

Another integer representing the number of coordinates in the next line in the drawing.另一个整数,表示绘图中下一行的坐标数。

The coordinates in that line, each pair separated by a newline.该行中的坐标,每对由换行符分隔。

...etc. ...等等。

For example:例如:

3 3

3 3

5.0 2.0 5.0 2.0

3.4 6.7 3.4 6.7

7.8 9.6 7.8 9.6

6 6

5.0 2.0 5.0 2.0

3.4 6.7 3.4 6.7

7.8 9.6 7.8 9.6

8.9 7.8 8.9 7.8

9.6 4.3 9.6 4.3

3.7 2.7 3.7 2.7

4 4

4.0 4.0 4.0 4.0

7.8 5.0 7.8 5.0

3.8 8.3 3.8 8.3

2.1 7.6 2.1 7.6

You get the idea.你明白了。

Here's my code to parse the file after it has been converted to a string:这是我在将文件转换为字符串后解析文件的代码:

function parseDat(contents) {
    var dirtyArray = contents.split("\n");
    var fileArray = dirtyArray.filter(function (n) { return (n !== undefined && n !== null && n !== ""); });


        var lineArray = new Array(parseInt(fileArray[i], 10) + 1);
        lineArray[0] = fileArray[i];

        i++;

        for(var j = 1; j < lineArray.length; j++) {
            lineArray[j] = new Array(fileArray[i]);
            console.log(fileArray[i]);
            console.log("2d array length = " + lineArray[j].length);
            i++;
            for(var k = 0; k < lineArray[j].length; k++, i++) {
                lineArray[j][k] = fileArray[i];
            }

        }

        return lineArray;


}

Basically, I check to see the length the second dimension array should be, which in the example file thing I gave should be 3 for the first line.基本上,我检查第二维数组的长度,在我给出的示例文件中,第一行应该是 3。 console.log(fileArray[i]) returns the correct number, but when I use lineArray[j].length, for some reason, the answer is always 1. I've checked lots of other sources for 2D arrays in Javascript and this method of checking the second dimension array's length always works for them. console.log(fileArray[i]) 返回正确的数字,但是当我使用 lineArray[j].length 时,出于某种原因,答案总是 1。我在 Javascript 中检查了很多其他二维数组的来源,这个检查第二维数组长度的方法始终适用于它们。

Please help, I know it's probably just some dumb mistake I'm making.请帮忙,我知道这可能只是我犯的一些愚蠢的错误。

Maybe you can "ignore" the problem.也许你可以“忽略”这个问题。 If console.log(fileArray[i]) really does give the correct number, you could just go for this in your following for-statement:如果console.log(fileArray[i])确实给出了正确的数字,您可以在以下 for 语句中使用它:

for(var k = 0; k < lineArray[i]-1; k++, i++) {
    lineArray[j][k] = fileArray[i];
}

-1 because you increment it with i++ beforehand. -1 因为你事先用 i++ 增加了它。 Or you just declare a new var before incrementing and use that in your for-statement.或者你只是在递增之前声明一个新的 var 并在你的 for 语句中使用它。 Might be a bit of a lazy cheaty way out, but your code might work just as intended.可能有点偷懒,但您的代码可能会按预期工作。

I'm an idiot and did not convert my strings into integers.我是个白痴,没有将我的字符串转换为整数。 Therefore I was setting my arrays equal in length to the string "3" (which is 1 long) instead of being 3 long.因此,我将数组的长度设置为等于字符串“3”(1 长)而不是 3 长。 The solution was to change解决方案是改变

lineArray[j] = new Array(fileArray[i]);

to

lineArray[j] = new Array(parseInt(fileArray[i], 10));

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

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