1

I am trying to read csv in typescript column by column.

The csv is like below:

Prefix,Suffix
Mr,Jr
Mrs,Sr

Based on few SO questions and typescript documentation, I am able to read from file like:

public readConfig() {
    const results: any[] = [];

    fs.createReadStream('D:/NameConfig.csv')
        .pipe(csv())
        .on('data', (data: any) => results.push(data))
        .on('end', () => {
            return results;
        });
}

But, this reads the entire csv. I am unable to save the individual columns like Prefix and Suffix as individual array. How can I achieve the same?

exaucae
  • 2,071
  • 1
  • 14
  • 24
Mike
  • 721
  • 1
  • 17
  • 44

1 Answers1

1

I have consumed the columns like below and have that added in a list and retrieved later.

public readConfig() {
    fs.createReadStream('D:/NameConfig.csv')
        .pipe(csv())
        .on('data', (data: any) => {
            prefixList.push(data["PREFIX"]);
            suffixList.push(data["SUFFIX"]);
        });
}
Mike
  • 721
  • 1
  • 17
  • 44