【问题标题】:NodeJs Readline value put into arrayNodeJs Readline值放入数组
【发布时间】:2016-02-13 22:54:48
【问题描述】:

我正在使用 NodeJS readline 模块来读取文本文件。它将每一行都返回到控制台,但是我希望它将返回的行添加到数组中。这是我到目前为止得到的代码,但它不起作用。我认为 rd.on 充当了一种 for 循环,但它看起来不像。任何帮助将不胜感激!

    function getinfomation2(){
        var firstfile = [];
        var secondfile = [];
        var countvar = 0;
        readline = require('readline');

        var rd = readline.createInterface({
            input: fs.createReadStream('../FixtureProfiles/CFIP1.json'),
            output: process.stdout,
            terminal: false
        });

        // Add fixture details from file to array to later be called
        rd.on('line', function(line) {
            // if fails console.log(line) returns line in console
            firstfile(countvar) = line;
            console.log(firstfile(countvar))
            countvar = countvar +1;
        });
    }

【问题讨论】:

  • firstfile(countvar) = line 不正确。数组访问运算符是[](参见here)。另请查看Array.push
  • 该死!你的权利。感谢您发现这一点。我完全被吓坏了,错过了!

标签: arrays node.js readline


【解决方案1】:

firstfile(countvar) = line 不正确。

数组访问运算符是[](请参阅Array docs on MDN)。

也可以查看Array.push:

firstfile.push(line)

【讨论】:

  • 谢谢,Up 完全错过了数组运算符。感谢您指出这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
  • 2017-05-19
  • 2020-01-08
  • 2017-11-19
  • 1970-01-01
相关资源
最近更新 更多