【问题标题】:extracting lines by comparing two lines through nodejs通过nodejs比较两行来提取行
【发布时间】:2018-08-24 00:16:51
【问题描述】:

假设我有一个包含以下行的文本文件...

显示

用户
D
电子
回应
F

我想通过 nodejs 提取数组中的值 D 和 E...我该如何实现?

【问题讨论】:

  • 比较在哪里?
  • 我如何比较用户和响应,以便我可以获得两者之间的值
  • 下次尝试明确您的要求。编辑您的问题,添加有关该问题的更多信息。您的问题中没有提示您希望用户和响应之间的结果。

标签: node.js file compare line extract


【解决方案1】:

这里有:

const fs = require('fs')
const readline = require('readline')

// we will save the result in this array
const result = []

let afterUser = false

const rl = readline.createInterface(fs.createReadStream('text.txt'))

const listener = rl.on('line', line => {
  // finish the listener if we reach Response line
  if (line === 'Response') {
    rl.close()
    return
  }

  // if we are after User line push the item to the array
  if (afterUser) {
    result.push(line)
  }

  // we'll set afterUser to true if we detect the User line
  if (!afterUser && line === 'User') {
    afterUser = true
  }
})

// after finish reading the file we can show the result
rl.on('close', () => {
  console.log(result)
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-09
    • 2020-04-11
    • 1970-01-01
    • 2019-10-17
    • 2015-04-26
    • 2023-04-07
    • 1970-01-01
    • 2015-02-05
    相关资源
    最近更新 更多