0

I want to parse a csv file into an array so I can use it to map data later.

I tried multiple libraries like csv-parser, fast-csv but for some reason the data doesn't get stored in the array (I believe it has to do with the fact that the method createReadStream is async)

The code below prints the data to the console but when I log it I get an undefined.

const fs = require('fs');
const csv = require('csv-parser');

let csvPath = './file.csv';

const results = [];

fs.createReadStream(csvPath)
    .pipe(csv())
    .on('data', (data) => results.push(data))
    .on('end', () => {
        console.log(results);
});

//prints 0
console.log(results.length);

1 Answers1

0

You can use Linq extension methods:

using System.Linq;

public List<Patient> GetPatients(Doctor doctor)
{
  return AllAppointments
         .Where(appointment => appointment.Doctor == doctor)
         .Select(appointment => appointment.Patient)
         .Distinct()
         .ToList();
}

It filters the full list to get all doctors matching the required doctor.

Next it selects all the patients of this doctor.

Then it selects distinct patients in case of duplicates.

And we convert the IEnumerable<> sequence result of the Linq query to a List<Patient> for the return of the method.