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

392
Views
¿Cómo mostrar el recuento total de elementos en función de dos o más condiciones en react js?

Estoy tratando de mostrar el recuento total de participantes en función de dos condiciones: tener "Pocu": "Da" y "Nume": "Contabilitate" de la matriz anidada debajo.

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

la salida es: 85 debería ser 20

Cualquier ayuda se agradece, gracias

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

0

Aquí, puede usar el filtro para colocar esos elementos en la matriz que cumplieron con estas dos condiciones y luego contar los elementos en la matriz.

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

0

por favor intente eso

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

salidas 1

Al buscar en la matriz Cursuri anidada, lo hacemos como se muestra a continuación.

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

o puedes hacerlo como abajo

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

Violín

about 4 years ago · Juan Pablo Isaza Report

0

Simplemente puede lograrlo iterando los objetos secundarios con la ayuda de Object.keys() junto con el método Array.forEach() .

Demostración en vivo :

 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 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!