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

128
Visualizações
Javascript make calculations and filter list

I have below array -

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
},
}

I want to filter and make calculations on this array such that percentage can be obtained.

Eg. veryLate + Late+ onTime = (3+5+2) = 10

So percentage wise it is -

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
},]

To calculate this I had performed below , but getting syntax error over brackets -

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

How can I obtained calculated results?

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

0

The map() method needs to return a value.

Try this

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 Relatório

0

x.veryLate wont work in x it should be veryLate itself same for the others

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)

You also must wrap the returning object literal into parentheses. Currently the curly braces is being denoted as the function body.

about 4 years ago · Juan Pablo Isaza Relatório

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;
}, []);

Throwing error because:

  1. The data array has no closing bracket
  2. map method is not returning anything. if you want to return an object from it use return keyword.
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