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
}
]
...
"/t"not"\t". You could also split by any whitespace using/\s/