Read CSV File In React

I'm uploading and then reading the CSV file but I'm facing an issue while splitting it, so basically, column names in CSV contain ',' so when I'm going to split the columns with ',' so I don't get the full column value, please suggest me some proper way for it. Thanks

const readCsv = (file) => {
    const reader = new FileReader();
    const filetext = reader.readAsBinaryString(file);
    reader.addEventListener('load', function (e) {
        const data = e.target.result;
        let parsedata = [];

        let newLinebrk = data.split('\n');
        for (let i = 0; i < newLinebrk.length; i++) {
            parsedata.push(newLinebrk[i].split(','));
        }
        console.log("parsedData: ", parsedata);
    });
};

CSV:

column 1   column2                    
test       lorem, ipsum, dummy/text

after splitting:

['test', 'lorem', 'ipsum', 'dummy/text']

so by doing that I'm unable to get a proper column name that contains a comma in string.


Solution 1:

In my case, I used Papa Parse which fulfills my all requirements.

const readCsv = (file) => {
    const reader = new FileReader();
    reader.readAsBinaryString(file);
    reader.addEventListener('load', function (e) {
        const data = e.target.result;
        Papaparse.parse(data, {
            complete: function (results) {
                console.log("results: ", results.data);
            },
        });
    });
};