Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

401
Visualizações
How to show total count of elements based on two or more conditions in react js?

I am trying to show total count of participants based on two conditions: have "Pocu": "Da" and "Nume": "Contabilitate" from below nested array.

let popular = JSON.parse(`[
   {
     "id": "15f806ec-79cf-498f-8a4d-8bc8fdf8c43e",
     "Nume": "negrea",
     "Prenume": "ioana",
     "Pocu": "Da",
     "createdAt": "2022-04-27T13:17:05.000Z",
     "updatedAt": "2022-04-27T13:17:05.000Z",
     "deletedAt": null,
     "INDICATORI": [
       {
         "id": "068170b3-7995-41df-8fac-4f2bc577e2c6",
         "Nume": "4S110",
         "importHash": null         
       }
     ],
     "Cursuri": [
        {
         "id": "068170b3-7995-41df-8fac-4f2bc577e2c6",
         "Nume": "Contabilitate",
         "importHash": null
         }
       ]
   }
]`);
   

const count = popular.reduce((total, participant) => total + Math.min (participant.Cursuri
  .filter(indicator => indicator.Nume === "Contabilitate").length, popular
  .filter(indicator => indicator.Pocu === "Da").length), 0)

console.log(count);

output is : 85 it shoud be 20

any help is appreciated, thank you

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Here, you can use filter to put those items into the array that met these two conditions and then count the items in the array.

const count = popular.filter(item =>  item.Pocu === "Da" && item.Nume === "Contabilitate").length
about 4 years ago · Juan Pablo Isaza Relatório

0

Please try that

const count = popular.filter(item => item.Pocu === "Da" && item.Cursuri.filter(c => c.Nume === "Contabilitate").length > 0).length;

console.log(count);

outputs 1

When searching in the nested Cursuri array we do it like below

if (item.Cursuri.filter(c => c.Nume === "Contabilitate").length > 0) {
}

or you can do it like below

if (item.Cursuri.some(c => c.Nume === "Contabilitate")) {
}

Fiddle

about 4 years ago · Juan Pablo Isaza Relatório

0

You can simply achieve it by iterating the child objects with the help of Object.keys() along with Array.forEach() method.

Live Demo :

let popular = JSON.parse(`[
   {
     "id": "15f806ec-79cf-498f-8a4d-8bc8fdf8c43e",
     "Nume": "negrea",
     "Prenume": "ioana",
     "Pocu": "Da",
     "createdAt": "2022-04-27T13:17:05.000Z",
     "updatedAt": "2022-04-27T13:17:05.000Z",
     "deletedAt": null,
     "INDICATORI": [
       {
         "id": "068170b3-7995-41df-8fac-4f2bc577e2c6",
         "Nume": "4S110",
         "importHash": null         
       }
     ],
     "Cursuri": [{
         "id": "068170b3-7995-41df-8fac-4f2bc577e2c6",
         "Nume": "Contabilitate",
         "importHash": null
         }]
   }]`);
   
let count = 0;

popular.forEach(obj => {
    Object.keys(obj).forEach(key => {
    if (typeof obj[key] === 'object' && obj[key] !== null) {
        obj[key].forEach(participant => {
        if (obj.Pocu === 'Da' && participant.Nume === 'Contabilitate') {
            count++;
        }
      })
    }
  });
});

console.log(count);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda