Reputation: 634
I have created a function for csv reader using react-file-reader. It´s working fine and data is successfully uploaded into the database but that csv file have more than 10 columns. I want only the first three and fifth column.
That´s my code
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);
}
Can you help me?
Upvotes: 0
Views: 2106
Reputation: 87231
You could add a column index array where to set which columns to pick, e.g.
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]];
}
Upvotes: 1