1

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-19\t163.3\t164.54\t162.74\t163.1\t158470476',
    open: undefined,
    high: undefined,
    low: undefined,
    close: undefined,
    volume: undefined
  }
]
[
  {
    date: '10-Jun-19\t165.31\t165.4\t164.37\t164.8\t105667060',
    open: undefined,
    high: undefined,
    low: undefined,
    close: undefined,
    volume: undefined
  }
]
... 
2
  • 1
    You're splitting by "/t" not "\t". You could also split by any whitespace using /\s/ Commented Jul 20, 2021 at 16:39
  • Use regex to replace any consecutive whitespaces by a comma, then break into commas. Commented Jul 20, 2021 at 16:48

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

Comments

1

This code should work:

        const fs = require('fs');
    const file = fs.readFileSync('data.txt');
    //Read Each line separately and add them to an array. and remove first line of your file;
    const array = file.split('\n').slice(1);

    let objects = [], i, data;
    for(let row of array){
        i = 0;
        //separate each line data and add them to data array
        data = row.split(/ +/);
        objects.push({
            Date: data[i],
            Open: data[++i],
            High: data[++i],
            Low: data[++i],
            Close: data[++i],
            Volume:data[++i]
        })
    }

4 Comments

It seems Date: data[i], shoud also be Date: data[i++],? Or the other lines should use preincrement.
@vsemozhebuty No, the Date value is in the first index of data array and the other lines should use increment
Yes, but Date: data[i], and Open: data[i++], use the same i as it is incremented after returning its value (see Increment (++)).
I understood what does you mean and i edited my code. Thank you. @vsemozhebuty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.