Example JavaScript code to parse CSV data

前端 未结 12 1710
梦谈多话
梦谈多话 2020-11-21 07:58

Where could I find some JavaScript code to parse CSV data?

12条回答
  •  粉色の甜心
    2020-11-21 08:21

    csvToArray v1.3

    A compact (645 bytes), but compliant function to convert a CSV string into a 2D array, conforming to the RFC4180 standard.

    https://code.google.com/archive/p/csv-to-array/downloads

    Common Usage: jQuery

     $.ajax({
            url: "test.csv",
            dataType: 'text',
            cache: false
     }).done(function(csvAsString){
            csvAsArray=csvAsString.csvToArray();
     });
    

    Common usage: JavaScript

    csvAsArray = csvAsString.csvToArray();
    

    Override field separator

    csvAsArray = csvAsString.csvToArray("|");
    

    Override record separator

    csvAsArray = csvAsString.csvToArray("", "#");
    

    Override Skip Header

    csvAsArray = csvAsString.csvToArray("", "", 1);
    

    Override all

    csvAsArray = csvAsString.csvToArray("|", "#", 1);
    

提交回复
热议问题