簡體   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