Skip to content
Advertisement

How to turn data from a txt file into an array of objects in Node

I’m new to Node, and I have a text file that has data like this

Date    Open    High    Low Close   Volume
11-Jun-19   163.3   164.54  162.74  163.1   158470476
10-Jun-19   165.31  165.4   164.37  164.8   105667060
7-Jun-19    163.85  164.95  163.14  164.8   188337725 
... 

I would like to create an array of objects like this

[{
    Date: "11-Jun-19",
    Open: 163.22,
    High: 164.28,
    Low: 163.05,
    Close: 163.88,
    Volume: 5854647
}, {
    Date: "12-Jun-19",
    Open: 163.22,
    High: 164.28,
    Low: 163.05,
    Close: 163.88,
    Volume: 5854647
}, {
    Date: "15-Jun-19",
    Open: 163.22,
    High: 164.28,
    Low: 163.05,
    Close: 163.88,
    Volume: 5854647
}] 

How can I do this? This was my attempt:

const lineReader = require('line-reader');

lineReader.eachLine('input.txt', function (line) {
  let results = [];
  let divide = line.split("   ");
  for (let i = 0; i < divide.length; i++) {
    let field = divide[i].split("/t");
    results.push({
      date: field[0],
      open: field[1],
      high: field[2],
      low: field[3],
      close: field[4],
      volume: field[5]
    });
  }
  console.log(results);
}); 

But this create an array for each object and I get all the data showing under date like this:

[
  {
    date: '11-Jun-19t163.3t164.54t162.74t163.1t158470476',
    open: undefined,
    high: undefined,
    low: undefined,
    close: undefined,
    volume: undefined
  }
]
[
  {
    date: '10-Jun-19t165.31t165.4t164.37t164.8t105667060',
    open: undefined,
    high: undefined,
    low: undefined,
    close: undefined,
    volume: undefined
  }
]
... 

Advertisement

Answer

You can try readline internal module, by the way (see this example), if your file is big and you do need line by line processing:

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

async function processLineByLine() {
  const fileStream = fs.createReadStream('test.txt');

  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity,
  });

  let headers = null;
  const data = [];

  for await (const line of rl) {
    const row = line.split(/s+/);

    if (headers === null) { // So this is the first line.
      headers = row;
    } else {
      const entry = {};
      for (let i = 0; i < row.length; i++) {
        const header = headers[i];
        const cell = row[i];

        entry[header] = header === 'Date' ? cell : Number(cell);
        // Or more generally:
        // const mayBeNumber = Number(cell);
        // entry[header] = Number.isNaN(mayBeNumber) ? cell : mayBeNumber;
      }
      data.push(entry);
    }
  }

  console.log(data);
}

processLineByLine();
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement