Search code examples
javascripthtmljquerypapaparse

How to exclude row from CSV with Papaparse


I have a CSV file and I used Papaparse to parse my data from it to a table in HTML. But I would like to exclude a row from my file; This is how it's written the CSV file:

"Name";"Email";"Telephone"
"[Lista] E.D. Elettronica Dedicata";"";""
"Andrea Ferrari";"[email protected]";"15"
"Claudio Podavite";"[email protected]";"20"
"Cristiano Mascheretti";"[email protected]";"13"
....

And I would like to exclude the second row:

"[Lista] E.D. Elettronica Dedicata";"";""

I cannot do it manually from the file because it's automatically created. I tried look at other question or the dock, but I only found out about header change header and not exclude row

This is how I form my table on HTML from JS (I took it from here):

function htmlTableGenerator(content) {
    let contatti = document.getElementById('contatti');

    let html = '<table id="contactsTable" class="table table-condensed table-hover table-striped" style="width:100%">';

    if (content.length == 0 || typeof(content[0]) === 'undefined') {
        return null
    } else {
        const header = content[0];
        const data = content.slice(1);

        html += '<thead>';
        html += '<tr>';
        header.forEach(function(colData) {
            html += '<th>' + colData + '</th>';
        });
        html += '</tr>';
        html += '</thead>';
        html += '<tbody>';

        data.forEach(function(row) {
            if (header.length === row.length) {
                html += '<tr>';
                row.forEach(function(colData) {
                    html += '<td>' + colData + '</td>';
                });
                html += '</tr>';
            }
        });

        html += '</tbody>';
        html += '</table>';
        // insert table element into csv preview
        contatti.innerHTML = html;
        // initialise DataTable
        initDataTable();
    }
}

Solution

  • Slice the array from index 2

    If you want to ignore the line after the headers, change this line:

    const data = content.slice(1);
    

    to this:

    const data = content.slice(2);
    

    There is no shame in reading the documentation for code you don't understand - especially if you don't understand. Here is the documentation for "slice":

    JavaScript: Array.slice()