2

Firstly, I use the CSV-parser from Node.js

first this code:

const parse = require("csv-parse/lib/sync");
const fs = require("fs");

const filePath = req.file.path;

const csvContent = fs.readFileSync(filePath);

const records = parse(csvContent, {
        columns: false,
        skip_empty_lines: true,
        skip_lines_with_error: true,
        delimiter: ";"
    });

Now I have the problem that equal columns names will not be parsed if column are true.. if columns are true the results in the console are:

const records = parse(csvContent, {
        columns: true,


[ { Referenz: 'Beispielwert1',
    Client: 'Beispielwert2',
    Clienthauptgruppe: 'Beispielwert3',
    Kontrahent: 'Beispielwert4',
    Kontrahentenhauptgruppe: 'Beispielwert5',
    Bank: 'Beispielwert6',
    Back_to_Back: 'Beispielwert7',
    Int_Ext: 'Beispielwert8',
    Valuta: 'Beispielwert9',
    Geschaeftsart: 'Beispielwert10',
    Kauf_Verkauf: 'Beispielwert11',
    Wrg: 'Beispielwert12',
    Diff_In_Hauswaehrung: 'Beispielwert13,
    Hauswaehrung: 'Beispielwert14',
    Marktwert_NPV: 'Beispielwert5' },
  { Referenz: 'Beispielwert1',
    Client: 'Beispielwert2',
    Clienthauptgruppe: 'Beispielwert3',
    Kontrahent: 'Beispielwert4',
    Kontrahentenhauptgruppe: 'Beispielwert5',
    Bank: 'Beispielwert6',
    Back_to_Back: 'Beispielwert7',
    Int_Ext: 'Beispielwert8',
    Valuta: 'Beispielwert9',
    Geschaeftsart: 'Beispielwert10',
    Kauf_Verkauf: 'Beispielwert11',
    Wrg: 'Beispielwert12',
    Diff_In_Hauswaehrung: 'Beispielwert13,
    Hauswaehrung: 'Beispielwert14',
    Marktwert_NPV: 'Beispielwert5' }]

if columns are false the results in the console are:

const records = parse(csvContent, {
        columns: false,

[ [ 'Referenz',
    'Client',
    'Clienthauptgruppe',
    'Kontrahent',
    'Kontrahentenhauptgruppe',
    'Bank',
    'Back_to_Back',
    'Int_Ext',
    'Valuta',
    'Geschaeftsart',
    'Kauf_Verkauf',
    'Wrg',
    'Diff_In_Hauswaehrung',
    'Hauswaehrung',
    'Marktwert_NPV' 
    'Wrg'],
  [ 'Beispielwert1',
    'Beispielwert2',
    'Beispielwert3',
    'Beispielwert4',
    'Beispielwert5',
    'Beispielwert6',
    'Beispielwert7',
    'Beispielwert8',
    'Beispielwert9',
    'Beispielwert10',
    'Beispielwert11',
    'Beispielwert12',
    'Beispielwert13',
    'Beispielwert14',
    'Beispielwert15'
    'Beispielwert16' ],
  [ 'Beispielwert1',
    'Beispielwert2',
    'Beispielwert3',
    'Beispielwert4',
    'Beispielwert5',
    'Beispielwert6',
    'Beispielwert7',
    'Beispielwert8',
    'Beispielwert9',
    'Beispielwert10',
    'Beispielwert11',
    'Beispielwert12',
    'Beispielwert13',
    'Beispielwert14',
    'Beispielwert15'
    'Beispielwert16' ]]

Here for example the column Wrg is the double column and if columns stands on: true.. it doesn't exist

Now I want the same form again as above mentioned if columns are true.. but how can I put multiple Arrays to one object if the multiple arrays are in one variable (records)?

Best regards

Frederic

3
  • I'd suggest adding your required result to your question, is this something like: { 'Referenz': ['Beispielwert1','Beispielwert1','Beispielwert1'], 'Client': ['Beispielwert2','Beispielwert2','Beispielwert2'] ... thanks! Commented Sep 20, 2019 at 7:49
  • 1
    my required result is the result that i get if the columns is 'true' but then for example the double column Wrg doesn't exist.. Therefore I need the same structure but now with the double column.. and i think one can not avoid the rebuilding because only if the columns is 'false' the double column exist. Commented Sep 20, 2019 at 8:10
  • Ah yes, thank you.. I understand now! Commented Sep 20, 2019 at 8:23

1 Answer 1

1

We can remap each row value, updating any duplicate fields with a unique number.. that will then change 'Wrg' to 'Wrg2' for example. You can also map to a single column, with an array of the fields present instead, I've included both approaches below:

const parse = require("csv-parse/lib/sync");
const fs = require("fs");

const filePath = req.file.path;
const csvContent = fs.readFileSync(filePath);

let records = parse(csvContent, {
    delimiter: ";",
    skip_empty_lines: true,
    skip_lines_with_error: true,
});

function getFieldNameArray(row) {
    return row.reduce((fieldNames, originalFieldName) => {
        let fieldName = originalFieldName;
        let uniqifier = 1;
        while (fieldNames.includes(fieldName)) {
            fieldName = `${originalFieldName}${++uniqifier}`; // You could use a different algorithm for this purpose.. e.g. use field_duplicate etc.. 
        }
        fieldNames.push(fieldName);
        return fieldNames;
    }, []);
}

const fieldNames = getFieldNameArray(records[0]);

records = records.slice(1).map((currentRow, index) => {
    return currentRow.reduce((prev, value, index) => {
        prev[fieldNames[index]] = value;
        return prev;
    }, {});
}, records)

console.log(records);

I get an output like so:

[ { Referenz: 'Beispielwert1',
    Client: 'Beispielwert2',
    Clienthauptgruppe: 'Beispielwert3',
    Kontrahent: 'Beispielwert4',
    Kontrahentenhauptgruppe: 'Beispielwert5',
    Bank: 'Beispielwert6',
    Back_to_Back: 'Beispielwert7',
    Int_Ext: 'Beispielwert8',
    Valuta: 'Beispielwert9',
    Geschaeftsart: 'Beispielwert10',
    Kauf_Verkauf: 'Beispielwert11',
    Wrg: 'Beispielwert12',
    Diff_In_Hauswaehrung: 'Beispielwert13',
    Hauswaehrung: 'Beispielwert14',
    Marktwert_NPV: 'Beispielwert15',
    Wrg2: 'Beispielwert16' }
]

If you wish to create an array of values under 'Wrg', this should work:

const parse = require("csv-parse/lib/sync");
const fs = require("fs");

const filePath = req.file.path;
const csvContent = fs.readFileSync(filePath);

let records = parse(csvContent, {
    delimiter: ";",
    skip_empty_lines: true,
    skip_lines_with_error: true,
});

let fieldNames = Object.values(records[0]);

records = records.slice(1).map((currentRow) => {
    return currentRow.reduce((prev, value, index) => {
        if (!prev.hasOwnProperty(fieldNames[index])) {
            prev[fieldNames[index]] = value;
        } else {
            let existing = prev[fieldNames[index]];
            prev[fieldNames[index]] = (Array.isArray(existing) ? existing: [existing]).concat(value);
        }
        return prev;
    }, {});
})


console.log(records);

I get an output like so:

[ { Referenz: 'Beispielwert1',
    Client: 'Beispielwert2',
    Clienthauptgruppe: 'Beispielwert3',
    Kontrahent: 'Beispielwert4',
    Kontrahentenhauptgruppe: 'Beispielwert5',
    Bank: 'Beispielwert6',
    Back_to_Back: 'Beispielwert7',
    Int_Ext: 'Beispielwert8',
    Valuta: 'Beispielwert9',
    Geschaeftsart: 'Beispielwert10',
    Kauf_Verkauf: 'Beispielwert11',
    Wrg: [ 'Beispielwert12', 'Beispielwert16' ],
    Diff_In_Hauswaehrung: 'Beispielwert13',
    Hauswaehrung: 'Beispielwert14',
    Marktwert_NPV: 'Beispielwert15' },
]
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! what do you mean with duplicate? I think with 'duplicate! the problem will occur again because the column name is equal
Yeah, I think you're right.. the other approaches will work very well in any case!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.