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

124
Views
Javascript hace cálculos y lista de filtros.

Tengo debajo de la matriz -

 const data=[ { month:"nov", veryLate:3, Late:5, onTime:2 }, { month:"dec", veryLate:1, Late:3, onTime:16 }, { month:"jan", veryLate:28, Late:1, onTime:1 }, }

Quiero filtrar y hacer cálculos en esta matriz para que se pueda obtener el porcentaje.

P.ej. muy tarde + tarde + a tiempo = (3+5+2) = 10

Entonces, en términos de porcentaje, es:

 const data= [ { month:"nov", veryLate:30, Late:50, onTime:20 }, { month:"dec", veryLate:5, Late:15, onTime:80 }, , { month:"jan", veryLate:98.33, Late:3.33, onTime:3.33 },]

Para calcular esto, lo hice a continuación, pero obtuve un error de sintaxis sobre los corchetes:

 var filteredData=data.map(x=>{ x.month, x.veryLate/(x.veryLate+x.Late+x.onTime)*100, x.Late/(x.veryLate+x.Late+x.onTime)*100, x.onTime/(x.veryLate+x.Late+x.onTime)*100, });

¿Cómo puedo obtener resultados calculados?

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

0

El método map() necesita devolver un valor.

Prueba esto

 var filteredData=data.map(x => { // get the original values to avoid repeating x. const {month, veryLate, Late, onTime} = x; // do your calculations const newVeryLate = veryLate / ( veryLate + Late + onTime) * 100; const newLate = Late / (veryLate + Late + onTime) * 100; const newOnTime = onTime / (veryLate + Late + onTime) * 100; // return the new object return {month, veryLate: newVeryLate, Late: newLate, onTime: newOnTime} });
about 4 years ago · Juan Pablo Isaza Report

0

x.veryLate no funcionará en x, debería ser veryLate mismo para los demás

 const data=[ { month:"nov", veryLate:3, Late:5, onTime:2 }, { month:"dec", veryLate:1, Late:3, onTime:16 }, { month:"jan", veryLate:28, Late:1, onTime:1 }, ] var filteredData= data.map(x => ( { ...x, veryLate: x.veryLate/(x.veryLate+x.Late+x.onTime)*100, Late: x.Late/(x.veryLate+x.Late+x.onTime)*100, onTime: x.onTime/(x.veryLate+x.Late+x.onTime)*100 }) ) console.log(filteredData)

También debe envolver el literal del objeto que regresa entre paréntesis. Actualmente, las llaves se denotan como el cuerpo de la función.

about 4 years ago · Juan Pablo Isaza Report

0

var filteredData=data.reduce((a, v)=>{ let obj = {}; let sum = v.veryLate+v.Late+v.onTime; obj.month = v.month; obj.veryLate = v.veryLate/sum*100; obj.Late = v.Late/sum*100; obj.onTime = v.onTime/sum*100; a.push(obj) return a; }, []);

Error de lanzamiento porque:

  1. La matriz de datos no tiene corchetes de cierre.
  2. El método map no devuelve nada. si desea devolver un objeto, use la palabra clave return.
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!