Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

parse csv javascript

//Suppose you have a CSV file data.csv which contains the data:

//NAME,AGE
//Daffy Duck,24
//Bugs Bunny,22

const csv = require('csv-parser')
const fs = require('fs')
const results = []; 

fs.createReadStream('data.csv')
  .pipe(csv())
  .on('data', (data) => results.push(data))
  .on('end', () => console.log(results));

 // [{ NAME: 'Daffy Duck', AGE: '24' },
//   { NAME: 'Bugs Bunny', AGE: '22' } ] 
Comment

Parsing CSV With Javascript

<script type="text/javascript">
    // ref: http://stackoverflow.com/a/1293163/2343
    // This will parse a delimited string into an array of
    // arrays. The default delimiter is the comma, but this
    // can be overriden in the second argument.
    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 + "|
?
|
|^)" +

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

                // Standard fields.
                "([^"" + strDelimiter + "
]*))"
            ),
            "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
                ){

                // Since we have reached a new row of data,
                // add an empty row to our data array.
                arrData.push( [] );

            }

            var strMatchedValue;

            // Now that we have our delimiter out of the way,
            // let's check to see which kind of value we
            // captured (quoted or unquoted).
            if (arrMatches[ 2 ]){

                // We found a quoted value. When we capture
                // this value, unescape any double quotes.
                strMatchedValue = arrMatches[ 2 ].replace(
                    new RegExp( """", "g" ),
                    """
                    );

            } else {

                // We found a non-quoted value.
                strMatchedValue = arrMatches[ 3 ];

            }


            // Now that we have our value string, let's add
            // it to the data array.
            arrData[ arrData.length - 1 ].push( strMatchedValue );
        }

        // Return the parsed data.
        return( arrData );
    }

</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: convert string to number javascript 
Javascript :: composer require friendsofcake/crud-json-api for cakephp3 version 
Javascript :: for loop value index react 
Javascript :: radiojquery 
Javascript :: jquery select2 how to make dont close after select 
Javascript :: jquery forEach is not a function 
Javascript :: How to disable reactive form submit button in Angular 
Javascript :: append element in a div as first child 
Javascript :: js for each item in array 
Javascript :: js strict mode 
Javascript :: get object value in node js 
Javascript :: jquery target partial id 
Javascript :: what is reactjs 
Javascript :: us phone number regex 
Javascript :: nodejs array buffer to buffer 
Javascript :: deprecation warning: value provided is not in a recognized rfc2822 or iso format. moment construction falls back to js date(), which is not reliable across all browsers and versions 
Javascript :: find in string javascript 
Javascript :: js for in 10 
Javascript :: how to install formik in react native 
Javascript :: type of data model mongodb 
Javascript :: how to read 2 dimensional array in javascript 
Javascript :: passing state in link react 
Javascript :: iiee i 
Javascript :: how to change the text using jquery on click 
Javascript :: convert number to boolean javascript 
Javascript :: unix timestamp to date javascript yyyy-mm-dd 
Javascript :: crear proyecto angular 
Javascript :: multiple click events in react 
Javascript :: currying javascript sum 
Javascript :: ajax request in javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =