Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
How include multiple criteria in COUNT ROWS with javascript

I have the next HTML code:



 <table border="1">
  <tr>
    <th>Nº</th>
    <th>NOMBRE</th>
    <th>AREA</th>
  </tr>
  <tbody id="curso">

    <tr>
        <td>1</td>
        <td>Jose</td>
        <td>Gramática</td>
    </tr>
    <tr>
        <td>2</td>
        <td>Luis</td>
        <td>Gramática</td>
    </tr>
    <tr>
        <td>3</td>
        <td>Andrea</td>
        <td>Gramática</td>
    </tr>

    <tr>
        <td>4</td>
        <td>Andrea</td>
        <td>Idiomas</td>
    </tr>

  </tbody>
</table>

<p id="Cantidad"></p>

</body>

This code can count the number of rows using a criteria in the AREA field:


<script>

function ContarFila() {

var cantidadFilas = document.getElementById("curso").rows.length;
 
  let resultados = {};
  let elementos = document.querySelectorAll(
    "table tbody tr > td:nth-child(3)"
  );

  elementos.forEach(elemento => {
    if (resultados.hasOwnProperty(elemento.innerText)) {
      resultados[elemento.innerText]++;
    } else {
      resultados[elemento.innerText] = 1;
    }
  });

  console.log(resultados);
  for (let indice in resultados) {
    document.getElementById("Cantidad").innerHTML =
      resultados['Gramática'];
  };
}

window.onload=ContarFila();

</script>

When loading the page and using the 'Gramática' criteria of the 'AREA' field, the result is 3.

How do I include MULTIPLE CRITERIA with the 'NOMBRE' and 'AREA' field at the same time?

Example:

Using Criteria 1: 'Grammar'

Using Criteria 2: 'Luis'

RESULT: 1

THAK YOU!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If I understood you correctly, then perhaps this example below can help you:

function getResult(table, search = {}) {
  const tableHeadCells = Array.from(table.tHead.rows[0].cells);
  const tableBodyRows = Array.from(table.tBodies[0].rows);
  const indexes = tableHeadCells.map((el) => el.dataset["label"]); // <= // ['index', 'nombre', 'area']

  const count = tableBodyRows.reduce((acc, row) => {
    isSearched = indexes.every(
      (key, i) => !search[key] || search[key] === row.cells[i].innerText
    );
    return isSearched ? ++acc : acc;
  }, 0);

  return count;
}

const table = document.querySelector('table');
console.log('1 => ', getResult(table, {
  area: 'Gramática'
}));
console.log('2 => ', getResult(table, {
  nombre: 'Luis',
  area: 'Gramática'
}));
console.log('3 => ', getResult(table, {
  index: '3',
  nombre: 'Andrea'
}));
<table border="1">
  <thead>
    <tr>
      <th data-label="index">Nº</th>
      <th data-label="nombre">NOMBRE</th>
      <th data-label="area">AREA</th>
    </tr>
  </thead>
  <tbody id="curso">
    <tr>
      <td>1</td>
      <td>Jose</td>
      <td>Gramática</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Luis</td>
      <td>Gramática</td>
    </tr>
    <tr>
      <td>3</td>
      <td>Andrea</td>
      <td>Gramática</td>
    </tr>
    <tr>
      <td>4</td>
      <td>Andrea</td>
      <td>Idiomas</td>
    </tr>
  </tbody>
</table>

Note that in the header of the table, the columns are indicated by data-attributes data-label in order to understand which column belongs to which keyword. And then, we just count the number of elements in the loop, which completely match the specified requirements for all columns

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!