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?

1 Answer 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"]);
        });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.