繁体   English   中英

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

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

我有一个像这样的 csv 文件:

标签.csv

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

想要使用 jQuery 读取此 csv 文件并打印到数组中以将此数据输入自动提示:

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

试试这个代码

 /* 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