简体   繁体   English

如何使用 jquery 读取 csv 文件并将数据打印到数组中

[英]how to read csv file using jquery and print data into an array

I have a csv file like this:我有一个像这样的 csv 文件:

tags.csv标签.csv

1140,ABCD
1142,ACBD
1144,ADCB
1148,DABC

Want to read this csv file using jQuery and print into an array to getting this data into input autosuggest:想要使用 jQuery 读取此 csv 文件并打印到数组中以将此数据输入自动提示:

   $( function() {
        var availableTags = ["ABCD","ACBD","ADCB","DABC",]; //want to print csv data into this array.
        $( "#tags" ).autocomplete({
        source: availableTags
        });
    } );

Try this code试试这个代码

 /* this function reads data from a file */ $(document).ready(function() { $.ajax({ type: "GET", url: "tags.csv", dataType: "text", success: function(data) { const parsedCSV = parseCSV(data) $( function() { var availableTags = parsedCSV; $( "#tags" ).autocomplete({ source: availableTags }); } ); } }) }) function parseCSV(csv) { /* split the data into array of lines of type */ const csvLines = csv.split(/\r\n|\n/); /* loop throw all the lines a remove first part (from the start, to comma) */ return csvLines.map(line => line.split(',')[1]) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM