Reputation: 171
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
}
]
...
Upvotes: 1
Views: 1043
Reputation: 36
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]
})
}
Upvotes: 1
Reputation: 13782
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();
Upvotes: 1