-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvparse.js
33 lines (27 loc) · 899 Bytes
/
csvparse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const fs = require('fs');
class CSVReader {
constructor() {
this.data = [];
}
readFile(filePath) {
const fileContent = fs.readFileSync(filePath, 'utf-8');
this.parseCSV(fileContent);
// Example: Get string value based on ID
const idToLookup = '123'; // Replace with the desired ID
const stringValue = this.getStringById(idToLookup);
console.log(stringValue);
}
parseCSV(csvData) {
this.data = csvData.split('\n').map(line => {
const [id, stringValue] = line.split(',');
return id && stringValue ? { id, stringValue } : null;
}).filter(Boolean);
}
getStringById(id) {
const matchingEntry = this.data.find(entry => entry.id === id);
return matchingEntry ? matchingEntry.stringValue : null;
}
}
// Example usage:
const csvReader = new CSVReader();
csvReader.readFile('path/to/your/file.csv');