JSFiddle - React, Tailwind, and code Playground

by Nayana Das

HTML

<h3>
CSV each column into seaparate array
</h3>
<textarea id="test-csv"></textarea><br>
<button id="parse-button" >Parse</button>

CSS

textarea {
    width: 600px;
    height: 300px;
}

JavaScript

$("#parse-button").click(function(){

var data=$('#test-csv').val();

	//alert(data);
  
  
  var csvarray=CSVToArray( data, "," );
  console.log(JSON.stringify(csvarray));
  var mainArr=[];
 for (var i = 0; i < csvarray.length; i++) {
 			/*console.log(csvarray[i]);
     var csvarray1=csvarray[i].map(Number);
     mainArr.push(csvarray1);*/
     
     for(var j=0; j <csvarray[i].length; j++){
         //console.log(csvarray[i][j]);
     }
}
  console.log(JSON.stringify(mainArr));
  
});

function CSVToArray( strData, strDelimiter ){

        // Check to see if the delimiter is defined. If not,
        // then default to comma.
        strDelimiter = (strDelimiter || ",");

        // Create a regular expression to parse the CSV values.
        var objPattern = new RegExp(
            (
                // Delimiters.
                "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

                // Quoted fields.
                "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

                // Standard fields.
                "([^\"\\" + strDelimiter + "\\r\\n]*))"
            ),
            "gi"
            );


        // Create an array to hold our data. Give the array
        // a default empty first row.
        var arrData = [[]];

        // Create an array to hold our individual pattern
        // matching groups.
        var arrMatches = null;


        // Keep looping over the regular expression matches
        // until we can no longer find a match.
        while (arrMatches = objPattern.exec( strData )){

            // Get the delimiter that was found.
            var strMatchedDelimiter = arrMatches[ 1 ];

            // Check to see if the given delimiter has a length
            // (is not the start of string) and if it matches
            // field delimiter. If id does not, then we know
            // that this delimiter is a row delimiter.
            if (
                strMatchedDelimiter.length &&
                strMatchedDelimiter !== strDelimiter
     ...