2
Var stdin = process.openStdin();
stdin.addListener("data",function(d){});

How can I fetch input from this? I've tried d.toString.trim(); But later when I push in the array it is also pushing \n in the array.

Mark
  • 999
  • 1
  • 7
  • 15
  • is it a node.js application? Because of process. – Aalexander Jan 16 '21 at 07:41
  • `Var` -> `var`, but probably should be `let`. That should also be `d.toString().trim()` as the function call brackets are absolutely necessary in JavaScript. – tadman Jan 16 '21 at 15:58
  • Why do you need `openStdin()`? It's already there as [`process.stdin`](https://nodejs.org/api/process.html#process_process_stdin) and you can immediately read from it. Remember you can `pipe()` it to things using streams. – tadman Jan 16 '21 at 15:59
  • Does this answer your question? [Read all text from stdin to a string](https://stackoverflow.com/questions/30441025/read-all-text-from-stdin-to-a-string) – tadman Jan 16 '21 at 16:00

1 Answers1

3

You can try this

var stdin = process.openStdin();
let arr=[];
stdin.addListener("data",function(d){

arr.push(d.toString().replace(/\r?\n|\r/g, " "));
console.log(arr);

});

See repl

Reference answer-for string replace

Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46