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

220
Views
¿Cómo cuento la cantidad de valores de verdad en una matriz?

Estoy luchando con algo de lógica, pero creo que estoy cerca. ¿Hay alguna forma de obtener el número de valores de verdad de una matriz de valores booleanos?

 const checkedState = [true, false, true] function handleCourseProgress() { //for each value that is true in the array... checkedState.forEach((value) => { //if the value is true... if (value === true) { //count and total them up here.... setCourseProgress(//return the count//); } }); }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

La forma más fácil es con reducir. Aquí está el ejemplo.

 const checkedState = [true, false, true] const answer = checkedState.reduce((acc, val) => val ? acc + val : acc); console.log(answer)

about 4 years ago · Juan Pablo Isaza Report

0

filter los elementos que son true y devuelve la longitud de esa matriz.

 const one = [false, true, true]; const two = [true, true, true]; const three = [false, false, false, false]; function trueElements(arr) { return arr.filter(b => b).length; } console.log(trueElements(one)); console.log(trueElements(two)) console.log(trueElements(three))

about 4 years ago · Juan Pablo Isaza Report

0

 const checkedState = [false, true, false, true, true] const count = checkedState.filter((value) => value).length // Cleaner way const anotherCount = checkedState.filter(Boolean).length console.log(count) console.log(anotherCount)

Básicamente, filtrar la matriz y buscar los valores verdaderos y verificar la longitud de la matriz hará el truco y luego puede llamar a setCourseProgress(count) con el valor de conteo correcto.

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!