Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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:

 [[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.

<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 . instead of the lines. What am I doing wrong?

like image 573
Spyros Avatar asked Jun 07 '13 17:06

Spyros


3 Answers

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));
  });
}
like image 162
Sly Avatar answered Sep 21 '22 22:09

Sly


Instead of using JSON, you can push the line onto the array as well:

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.
like image 32
Skyjob Avatar answered Sep 22 '22 22:09

Skyjob


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. In JavaScript, any variable can hold any kind of value, and you're replacing an array with a string. 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:

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 is the array you want, I think, if I understand what you're doing. 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.

Or if you want to parse the rows as JSON objects,

nodeArray = new Array();

for ( var i = 0; i < arrLines.length; ++i )
{
    nodeArray.push(JSON.parse(arrLines[i]));
}
like image 42
15ee8f99-57ff-4f92-890c-b56153 Avatar answered Sep 23 '22 22:09

15ee8f99-57ff-4f92-890c-b56153