ข้อมูล csv ของฉันมีลักษณะเช่นนี้:
Heading1, heading2, heading3, heading4, heading5, value1_1, value2_1, value3_1, value4_1, value5_1, value1_2, value2_2, value3_2, value4_2, value5_2 ....
คุณอ่านข้อมูลนี้และแปลงเป็นอาร์เรย์อย่างนี้ได้อย่างไรโดยใช้ Javascript:
[ส่วนหัว 1: ค่า 1_1, ส่วนหัว 2: ค่า 2_1, ส่วนหัว 3: ค่า 3_1, ส่วนหัว 4: ค่า 4_1, ส่วนหัว 5: ค่า 5_1], [ส่วนหัว 1: ค่า 1_2, ส่วนหัว 2: ค่า 2_2, ส่วนหัว 3: ค่า 3_2, ส่วนหัว 4: ค่า 4_2]
ฉันได้ลองใช้รหัสนี้แล้ว แต่ไม่มีโชค!:
<script type="text/javascript">
var allText =[];
var allTextLines = [];
var Lines = [];
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "file://d:/data.txt", true);
txtFile.onreadystatechange = function()
{
allText = txtFile.responseText;
allTextLines = allText.split(/\r\n|\n/);
};
document.write(allTextLines);<br>
document.write(allText);<br>
document.write(txtFile);<br>
</script>
file://...
XMLHttpRequest