Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda