This commit is contained in:
@@ -8,6 +8,28 @@ const quotedCSV = '"very big, soft computer mouse","the cutest peripheral ever",
|
||||
|
||||
const mixedCSV = '"very big, soft computer mouse","the cutest peripheral ever",10,39.90';
|
||||
|
||||
const parseCSVRecord = (record) => {
|
||||
const fields = [];
|
||||
let field = '';
|
||||
let inQuotes = false;
|
||||
|
||||
for (const character of record) {
|
||||
if (character === '"') {
|
||||
inQuotes = !inQuotes;
|
||||
} else if (character === ',' && !inQuotes) {
|
||||
fields.push(field);
|
||||
field = '';
|
||||
} else {
|
||||
field += character;
|
||||
}
|
||||
}
|
||||
|
||||
fields.push(field);
|
||||
return fields;
|
||||
};
|
||||
|
||||
console.log(parseCSVRecord(quotedCSV));
|
||||
console.log(parseCSVRecord(mixedCSV));
|
||||
/*
|
||||
Übung 25: CSV-Datensätze mit Anführungszeichen
|
||||
Bisher haben wir die Felder in einem CSV-Datensatz einfach mit der Anweisung:
|
||||
|
||||
Reference in New Issue
Block a user