I am attempting to populate a selection tag with just one column from a csv file.
It looks like I can get the 3rd column, but only the first letter appears for each option and a couple extra "." with an undefined at the end.
How to fix this so just the word is populated?
var csvFile = new XMLHttpRequest();
var select = document.getElementById('select');
csvFile.onload = function() {
csv_Text = csvFile.responseText;
csv_TextLines = csv_Text.split(/rn|n/);
var string = "";
for (var i = 0; i < csv_TextLines.length; i++) {
string = string + "<option>" + csv_TextLines[i][2] + "</option>"
}
document.getElementById('select').innerHTML = string;
}
csvFile.open("get", "export.csv", true);
csvFile.send();
<h1>hello</h1>
<select name="select" id="select">select...</select>
id,first_name,color,age,price
1,Richard,Blue,27,126.95
2,Layla,Russian violet,41,23.99
3,Kimberly,Rose ebony,48,172.95
4,Emily,Heliotrope,46,86.99
5,Anna,French lime,49,117.99
6,Julian,Dartmouth green,22,92.99
7,Jeremy,Blue,66,142.95
8,Jeremy,Fuchsia,43,36.95
9,Jennifer,Languid lavender,51,161.95
10,Evelyn,Blue,47,186.99
3
Answers
Each text line is a string, so
csv_TextLines[i][2]
will return the 3rd character of each line.What you need is another split with
,
and getting the 3rd element from it.Pseudo-code:
Within the loop if you add
console.log(csv_TextLines[i])
you will see that each item here is a string. You need to run.split(',')
on each line to split that line up by the comma characters into an array.split(",")
on each row to retrieve the individual cells