使用Java脚本将文本文件读取并存储到数组中 [英] Reading and storing a text file into an array with Javascript

查看:130
本文介绍了使用Java脚本将文本文件读取并存储到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想逐行拆分文本文件,然后在遇到行(':')中的字符时将其存储在数组中.我需要该数组才能在地图上绘制点.当我使用静态数组进行绘制时,可以做得很好.

  var map = new google.maps.Map(document.getElementById('map'),googleMap);var infowindow = new google.maps.InfoWindow();google.maps.event.addListener(infowindow,"closeclick",function(){map.fitBounds(latlngbound);map.panToBounds(latlngbound)})var ipArray = ["70.177.167.189","123.135.107.115","123.135.107.115","123.135.107.115","123.135.107.115","122.182.6.19","24.19.187.145","24.19.187.145","24.19.187.145","93.42.228.21","82.102.2.210"]];ipArray.forEach((ip)=> {addIPMarker(ip);})}抓住{e} {//处理错误} 

但是,当我遵循这种方法时,我无法做到这一点-

  var map = new google.maps.Map(document.getElementById('map'),googleMap);var infowindow = new google.maps.InfoWindow();google.maps.event.addListener(infowindow,"closeclick",function(){map.fitBounds(latlngbound);map.panToBounds(latlngbound)})var data_ip = [];var textByLine = [];readTextFile("./2017-12-12.txt",data_ip);var fs = require("fs");fs.readFile("./2017-12-12.txt",函数(文本){textByLine =(text.split("\ n")).split(:")[0];});textByLine.forEach((ip)=> {addIPMarker(ip);})}抓住{e} {//处理错误} 

任何人都可以告诉我我要去哪里哪里以及如何更正我的代码.我对JavaScript不太熟悉.因此,我不太确定自己到底在做什么.任何帮助表示赞赏!谢谢!

解决方案

这假定您正在使用节点的 fs 模块.

fs.readFile 函数是异步的,您可以正确地将处理过程处理到调用其结果的回调中的行中.

但是,在执行回调之前,您仍在使用已处理的 textByLine .届时,该数组将仍然为空.

您可以在拆分文件内容之后立即将 textByLine.forEach 部分移至回调中,也可以使用 readFileSync 切换到文件读取的同步版本./p>

同步版本将直接返回内容,而不使用方便的回调,但是它将阻止脚本的任何剩余部分的执行,直到读取文件为止.在处理可能较大的文件时,这通常不利于用户体验,因此首选异步版本.

回调确实有两个参数(请参见 fs.readFile文档): err data .如果设置了 err ,则表示由于某种原因读取失败.实际数据作为第二个参数提供.

最后,您的回调当前将输入拆分为几行,然后尝试使用':'分隔符将结果数组拆分.但是,您指出您希望每行都由定界符进行拆分,因此拆分行应在textByLine.forEach回调内部完成.

您的回调因此可以像这样进行修改:

  function(err,text){如果(错误){//以某种方式处理错误返回;}text.split('\ n').forEach((line)=> {让ip = line.split(:")[0];addIPMarker(ip);}} 

I want to split a text file line by line, and then on encountering a character in the line (':'), to store it in an array. I need that array to plot points on a map. I can do the plotting fine when I do it with a static array.

var map = new google.maps.Map(document.getElementById('map'), googleMap);
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(infowindow, "closeclick", function() {
    map.fitBounds(latlngbound);
    map.panToBounds(latlngbound)
    })
var ipArray = ["70.177.167.189", "123.135.107.115", "123.135.107.115", "123.135.107.115", "123.135.107.115", "122.182.6.19", "24.19.187.145", "24.19.187.145", "24.19.187.145", "93.42.228.21", "82.102.2.210"];

ipArray.forEach((ip) => {
addIPMarker(ip);
})
} catch(e){
    //handle error
}

However, I'm not able to do it when I follow this methodology -

var map = new google.maps.Map(document.getElementById('map'), googleMap);
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(infowindow, "closeclick", function() {
        map.fitBounds(latlngbound);
        map.panToBounds(latlngbound)
})
var data_ip = [];
var textByLine = [];
readTextFile("./2017-12-12.txt", data_ip);
var fs = require("fs");
fs.readFile("./2017-12-12.txt", function(text){
textByLine = (text.split("\n")).split(":")[0];
});

textByLine.forEach((ip) => {
addIPMarker(ip);
})
} catch(e){
    //handle error
}  

Can anyone tell me where I'm going wrong, and how I can correct my code. I'm not very familiar with JavaScript. So, I'm not really sure what exactly I'm doing here. Any help appreciated! Thanks!

解决方案

This presumes that you are using the fs module of node.

The fs.readFile function is asynchronous and you correctly do the processing into lines in the callback that is called with its result.

You are however still using the processed textByLine before the callback has executed. At that point, the array would still be empty.

You can either move the textByLine.forEach part into the callback, right after splitting the file content, or switch to the synchronous version of file reading using readFileSync.

The synchronous version would return the content directly and not use a callback which is convenient, it will however block the execution of any remaining part of the script until the file has been read. This is usually bad for the user experience when processing possibly large files and therefore the asynchronous versions are preferred.

The callback does get two arguments (see the fs.readFile documentation): err and data. If err is set, it means that reading failed for some reason. The actual data is supplied as the second argument.

Last, your callback currently splits the input into lines, and then tries to split the resulting array by the ':' delimiter. But you indicate that you want each line to be split by the delimiter, so splitting the line should be done inside the textByLine.forEach callback.

Your callback could therefore be adapted like this:

function(err, text) {
    if (err) {
        // handle error somehow
        return;
    }

    text.split('\n').forEach((line) => {
        let ip = line.split(":")[0];
        addIPMarker(ip);
    }
}

这篇关于使用Java脚本将文本文件读取并存储到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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