Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

82
Vistas
How to add two decimal places to an array of objects

The array looks like this:

const arr = [
    {title: 'Cat1', val1: '0', val2: '20'},
    {title: 'Cat2', val1: '0', val2: '30'},
]

I need the val1 and val2 to be converted into a number with two decimal places (eg. 0.00, 20.00), and then be able to pass the treated array (that includes everything else) in a different function.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

    const arr = [
        {title: 'Cat1', val1: '0', val2: '20'},
        {title: 'Cat2', val1: '0', val2: '30'},
    ]

    const result = arr.map(a1 => {
        a1.val1 += ".00"
        a1.val2 += ".00"
        return a1
    })

    console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

const arr = [{
    title: 'Cat1',
    val1: '0',
    val2: '20'
  },
  {
    title: 'Cat2',
    val1: '0',
    val2: '30'
  },
]

function print(data) {
  console.log(data);
}

function convertToDecimal(data) {
  let num = parseInt(data);
  return num.toFixed(2);
}
const converteedArray = arr.map((obj1) => {

  if (obj1.val1) {
    obj1.val1 = convertToDecimal(obj1.val1);
  }
  if (obj1.val2) {
    obj1.val2 = convertToDecimal(obj1.val2);
  }
  return obj1;
});
print(converteedArray);

Please check the above working example.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda