简体   繁体   English

如何跳过 Node.js readline 中的第一行?

[英]How to skip the first line in Node.js readline?

I want to read a file and skip its first line in an async function.我想读取一个文件并在异步 function 中跳过它的第一行。 I've tried rl.next() and rl.once('line',()=>{}) , and those don't work.我已经尝试过rl.next()rl.once('line',()=>{}) ,但这些都不起作用。 How to skip the first line?如何跳过第一行?

const fs = require('node:fs');
const readline = require('node:readline');

async function readfile(filename){
  const rs = fs.createReadStream(filename);
  const rl = readline.createInterface({input: rs});
  // not working: rl.next(), or rl.once('line',()=>{})
  for await (const line of rl) {

  }
}

Related question: the document says rl is an AsyncIterator , why doesn't it have a next() method?相关问题:文档rlAsyncIterator ,为什么它没有next()方法?

To keep it easy:为了简单起见:

async function readfile(filename){
  const rs = fs.createReadStream(filename);
  const rl = readline.createInterface({input: rs});

  let index = 0;
  for await (const line of rl) {
     if (index === 0 {
        continue;
     }
      
     // do what you have to do
     
     index++;
  }
}

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

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