![](/img/trans.png)
[英]React Js: How to read a csv file? (only 4 columns out of all columns)
[英]How read csv file only specific columns using javascript
我已經使用react-file-reader為csv閱讀器創建了一個函數。 它工作正常,數據已成功上傳到數據庫,但是csv文件有10列以上。 我只需要前三列和第五列。
那是我的代碼
const reader = new FileReader();
reader.readAsText(files[0]);
reader.onload = (x) => {
console.log('check data 1 :', x);
console.log('check data :', x.currentTarget.result.split('/n'));
const lines = x.currentTarget.result.split('\n');
const result = [];
for (let i = 0; i < lines.length - 1; i++) {
const obj = {};
//const newObj = lines.splice(4,1);
const currentline = lines[i].split(',');
const headers = ['ac_no', 'name', 'date_time', 'check_type'];
for (let j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
你能幫助我嗎?
您可以在其中設置要選擇的列的位置添加列索引數組,例如
const headers = ['ac_no', 'name', 'date_time', 'check_type'];
const columns = [0, 1, 2, 4];
for (let j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[columns[j]];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.