简体   繁体   English

Javascript:解析txt文件,将数据传递给数组

[英]Javascript: Parsing a txt file, passing the data to an array

Good evening. 晚上好。 I am really new to Javascript and I have been taking some courses to help me with a little side-project I have with a friend. 我对Javascript真的很陌生,我一直在学习一些课程,以帮助我完成与朋友一起做的一个小项目。

My problem is, I use Javascript to read a txt file (which I generate via an algorithm in a Java program offline) which has the following form: 我的问题是,我使用Javascript读取txt文件(该文件是通过Java程序离线中的算法生成的),其格式如下:

 [[37.928],[23.6965]]
 [[37.9305],[23.69675]]
 [[37.9315],[23.69775]]
 [[37.9335],[23.69975]]
 [[37.9350],[23.69999]]

The following Javascript tries to parse that file and return to me those values as a an array, with each line being a row in the array. 以下Javascript尝试解析该文件,并将这些值作为一个数组返回给我,每一行都是该数组中的一行。

<iframe id="frmFile" src="test.txt" onload="LoadFile();" style="display: none;"></iframe>
<script type="text/javascript">

var nodeArray=new Array();  

function LoadFile() {
    var oFrame = document.getElementById("frmFile");
    var strRawContents = oFrame.contentWindow.document.body.childNodes[0].innerHTML;
    while (strRawContents.indexOf("\r") >= 0)
        strRawContents = strRawContents.replace("\r", "");
    arrLines = strRawContents.split("\n");
    for (var i = 0; i < arrLines.length; i++) {
        nodeArray = arrLines[i];
    }
}

The problem is, if i leave the final line out and try to print the results, it comes normally (the whole line), when I try to fill the array it only enters one character in each row, the final result being [ [ 3 7 . 问题是,如果我省略最后一行并尝试打印结果,它通常会出现(整行),当我尝试填充数组时,它只会在每一行中输入一个字符,最终结果是[[3 7。 instead of the lines. 而不是线条。 What am I doing wrong? 我究竟做错了什么?

This should work: 这应该工作:

var nodeArray=new Array();  

function LoadFile() {
  var oFrame = document.getElementById("frmFile");
  var strRawContents = oFrame.contentWindow.document.body.childNodes[0].innerHTML.replace(/\r/g, '');
  var arrLines = strRawContents.split("\n");
  arrLines.forEach(function (line) {
    nodeArray.push(JSON.parse(line));
  });
}

It looks like you're assigning each string to nodeArray in turn, and then I assume you're looping through nodeArray later on and looking at nodeArray[0], nodeArray[1], etc. When you do that, nodeArray[0] will be the first character in the string , not the first string in an array. 看来您正在依次将每个字符串分配给nodeArray,然后我假设您稍后将遍历nodeArray并查看nodeArray [0],nodeArray [1]等。当您这样做时,nodeArray [0]将是字符串中的第一个字符 ,而不是数组中的第一个字符串。 In JavaScript, any variable can hold any kind of value, and you're replacing an array with a string. 在JavaScript中,任何变量都可以保留任何类型的值,并且您正在用字符串替换数组。 An array is a series of objects (strings, in this case), and a string is a series of characters. 数组是一系列对象(在这种情况下为字符串),而字符串是一系列字符。 You access the successive elements in either one using the same syntax. 您可以使用相同的语法访问任一元素中的后续元素。

If you want to add each line to nodeArray, do that like so: 如果要将每行添加到nodeArray,请执行以下操作:

for ( var i = 0; i < arrLines.length; ++i )
{
    nodeArray.push(arrLines[i]);
}

...but you don't even need to copy arrLines to another array. ...但是您甚至不需要将arrLines复制到另一个数组。 arrLines is the array you want, I think, if I understand what you're doing. 我想,arrLines是您想要的数组,如果我了解您在做什么。 So just do this: 因此,只需执行以下操作:

nodeArray = strRawContents.split("\n");

And skip the loop. 并跳过循环。 In that case, don't bother assigning new Array() to nodeArray at the beginning, since you'll be replacing that value with the other array anyhow. 在那种情况下,不要在一开始就为nodeArray分配新的Array(),因为无论如何您将用另一个数组替换该值。

Or if you want to parse the rows as JSON objects, 或者,如果您要将这些行解析为JSON对象,

nodeArray = new Array();

for ( var i = 0; i < arrLines.length; ++i )
{
    nodeArray.push(JSON.parse(arrLines[i]));
}

Instead of using JSON, you can push the line onto the array as well: 除了使用JSON,您还可以将行压入数组:

var arrLines = strRawContents.split("\n");

for (var i = 0; i < arrLines.length; i++) {
    nodeArray.push(arrLines[i]);
}

console.log( arrLines ); // see what we have here.

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

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