I have a csv file like this:
tags.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:
$( 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]) }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.