event-stream.js我如何参考每一行I.E(0),(1)等

时间:2014-04-03 20:25:38

标签: javascript arrays node.js event-stream

好吧,我使用事件流模块在我的node.js脚本中有一个函数。

我可以使用下面的简单脚本检查行数据,但我希望能够使用 每一行也是如此。我想要的例子

line(0)  --- first line of stdout 
line(1)  --- second line of stdout
ect, ect

我目前使用的剧本

function pinstripe() {
    es.pipeline(
    child.stdout,
    es.split(),
    es.map(function (line) {
        console.log(line);
        if (line === 'Captchadone') {
        child.stdin.write("hello" + '\n');


        }

        if (line === 'Input1') {
        child.stdin.write("243534:fdghdfgth456:45365" + '\n');


        }

    })
);   
}

然后我称之为

pinstripe();

这可以预期,但我如何使功能,所以我可以像这样使用..

function pinstripe() {
    es.pipeline(
    child.stdout,
    es.split(),
    es.map(function (line) {
        console.log(line);
        if (line === 'Captchadone') {
        child.stdin.write("hello" + '\n');
        child.stdin.write(line(0));   //i want to refrence
        child.stdin.write(line(1));   //each line like so 

        }

        if (line === 'Input1') {
        child.stdin.write("243534:fdghdfgth456:45365" + '\n');
        child.stdin.write(line(2));   //and here


        }

    })
);   
}

显然这不起作用,但我怎么能做到这样才能起作用

1 个答案:

答案 0 :(得分:0)

试试这个:

function pinstripe() {
  var index = 0,
      lines = [];

  es.pipeline(
    child.stdout,
    es.split(),
    es.map(function (line) {
      console.log(line);
      if (index <= 2) {
        lines[index++] = line;
      }
      if (line === 'Captchadone') {
        child.stdin.write("hello" + '\n');
        child.stdin.write(lines[0]);
        child.stdin.write(lines[1]);
        // set index = 0 to start over
      }
      if (line === 'Input1') {
        child.stdin.write("243534:fdghdfgth456:45365" + '\n');
        child.stdin.write(lines[2]);
        // set index = 0 to start over
      }
    })
  );
}