'Typescript - Read CSV column by column

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?



Solution 1:[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"]);
        });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mike