javascript - 如何将文件的每一行传输到数组单元格?

标签 javascript node.js

我尝试读取带有 id 的文件并将其作为数组返回。每行包含一个 id。我想将每个 id 传输到一个数组单元格。这是我的代码:

var fs = require('fs');
var reader = fs.createReadStream('RTIdList.js', {encoding: 'utf8'});
reader.once( 'end' ,  function ()  { 
    console.log( 'Read completed successfully.' ); 
});

var lineReader = require('readline').createInterface({
  input: reader,
});

function RTIdReader(){
  var arrayPos = 0;
  var idArray = new Array();

  lineReader.on('line', function (line) {
    console.log('Line from file:', line); 
    idArray[arrayPos] = line;
    arrayPos++;
  });

  console.log('idArray[0]: '+idArray[0]);
  return idArray;
}

RTIdReader();  

你知道我做错了什么吗?怎样才是正确的呢?

@DrakaSAN:代码一直有效,直到填满数组为止。我无法 console.log 或返回数组。代码在 lineReader.on 之后停止

大约两周以来,我一直在为回调而苦苦挣扎。我猜,我不明白他们的意思。这是我的尝试:

var fs = require('fs');

function RT(idList){
  console.log('RT works');
}

function idList(){
  var idArray = new Array();
  var reader = fs.createReadStream('RTIdList.js', {encoding: 'utf8'});
  reader.once( 'end' ,  function ()  { 
      console.log( 'Read completed successfully.' ); 
  });

  var lineReader = require('readline').createInterface({
    input: reader,
  });

  return {
    lineReader.on('line', function (line) {
      console.log('Line from file:', line); 
      idArray.push(line);
      return idArray;
    }
  }
}  

RT();

为什么我的回调不起作用?

好吧,回到开头。这就是我开始的地方。

var fs = require('fs');

var idArray = new Array();
var reader = fs.createReadStream('RTIdList.js', {encoding: 'utf8'});
reader.once( 'end' ,  function ()  { 
  console.log( 'Read completed successfully.' ); 
});

var lineReader = require('readline').createInterface({
  input: reader,
});

lineReader.on('line', function (line) {
  console.log('Line from file:', line); 
  idArray.push(line);
});

除了回调之外,我必须在哪里以及如何进行?

最佳答案

在向数组添加数据时使用push,这样做的方式就是像时尚一样向数组中的对象添加属性。差异很小,但稍后可能会咬你的屁股。

检查readline文档来学习如何使用它,您根本不需要阅读器。

您的返回发生在代码开始读取文件的同时,您需要检查异步函数是如何工作的。

工作代码提示:

您根本不需要 fs.createReadStream。

包含数组中所有 id 的回调将位于 .on('close', function () {/*here*/});

编辑:

在 JavaScript 中,您可以传递函数作为参数,并创建匿名函数。

因此,您不在异步代码中使用 return,而是将其余代码作为参数传递,按照惯例,该参数称为 callback

至于如何使用readline来读取文件,一个很好的例子是in the documentation可以在 5 分钟内将其转换为适合您的用例。

编辑:

好的,我已经花时间编写代码了。

(arg) => {}function (arg) {}

相同
const readline = require('readline');
const fs = require('fs');

function RT(file, callback) {
  let idArray = [];     //Declare idArray

  const rl = readline.createInterface({  //Set and start readline
    input: fs.createReadStream(file)
  });

  rl.on('line', (line) => { //For each line
    console.log('Line from file:', line);
    idArray.push(line);   //Add it to the array
  });

  rl.on('close', () => {  //At the end of the file
    callback(idArray);    //Call the rest of the code
  });
}

RT('RTIdList.js', (idArray) => {
  console.log('Here is the idArray: ' + idArray);
});

关于javascript - 如何将文件的每一行传输到数组单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37884633/

相关文章:

javascript - NodeJS 与 Express Framework - js 脚本的相对路径

javascript - Bootstrap 3 弹出窗口显示一个并隐藏其他不起作用

php - 清理数据以在引号内和 URL 内使用

node.js - 创建同步类别和 channel

node.js - 如何解决 'npm should be run outside of the node repl, in your normal shell'

node.js - api 路由无法通过 https 运行

node.js - Node 的事件循环阶段回调

javascript - 如果浏览器在 asp .net 中关闭,请从浏览器注销?

来自对象数组的 Javascript/Lodash 深度比较对象

javascript - 如何使javascript/jquery代码防错?