1

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";"andreaferrari@ed-elettronica.it";"15"
"Claudio Podavite";"claudiopodavite@ed-elettronica.it";"20"
"Cristiano Mascheretti";"cristianomascheretti@ed-elettronica.it";"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();
    }
}
nano
  • 65
  • 6

1 Answers1

1

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()

Peter Krebs
  • 3,831
  • 2
  • 15
  • 29
  • 1
    You saved me! And you are also very kind, thnks! – nano Apr 12 '23 at 13:20
  • Can I ask you one more thing? – nano Apr 12 '23 at 13:31
  • Sure - but you can also ask a new question if it's something bigger. – Peter Krebs Apr 12 '23 at 13:31
  • Okay! Let's see, in case I'm gonna create a new one. In my CSV file It parse also email. Is there a way to set ` – nano Apr 12 '23 at 13:34
  • Well the E-Mail is on index 1 of the dara, correct? Look up the [documentation for Array.forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach). It tells you the function can take a second parameter of the index. Instead of `row.forEach(function(colData) {` you would write `row.forEach(function(colData, index) {` and check for `index === 1` to print the E-Mail link. There are other ways but this seems easiest for you. – Peter Krebs Apr 12 '23 at 13:39
  • 1
    Okau. I'm gonna give a look at the doc you link me. Thanks you so much for your time! – nano Apr 12 '23 at 13:40