Problem
The push() function inside the createReadStream does not push my data to the array.
The Code
let csv = require('csv-parser');
let people = [];
let final_array = [];
fs.createReadStream('people.csv')
.pipe(csv())
.on('data', (data) => {
people.push(data)
})
.on('end', () => {
console.log(people) //This prints out people fine
})
console.log(people) //This returns empty array
people.forEach((names) => {
console.log(people[names]); //absolutely no output
})
The Output
[] //Output for console.log(people) outside the createReadStream
//Output for console.log people inside the createReadStream
[
{ username: 'rezero' },
{ username: 'software' },
{ username: 'planets' },
{ username: 'darkseid' },
{ username: 'gintama' },
{ username: 'websines' },
{ username: 'digital' }
]
Desired Output
I want only the usernames from the data to go into my final_array for further tasks.
final_array = ['rezero','software','planets','darkseid','gintama','websines','digital']