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

205
Vistas
sum of two array in Node.js

I try to Sum below two Arrays and use below two Methods:


    A = [
                    25.8,                 27,
      24.099999999999998, 30.070000000000004,
                    34.3, 34.300000000000004,
                    34.3,                 33,
                    29.2,               29.2,
                    27.6, 28.999999999999996,
      29.310000000000002, 27.000000000000004
    ]
    
    B = [
      '0.00387000', '0.00472000',
      '0.00534000', '0.00460000',
      '0.00060000', '0.00032000',
      '0.00053000', '0.00327000',
      '0.00217000', '0.00217000',
      '0.00460000', '0.00415000',
      '0.00852000', '0.02241000'
    ]

Method 1


    //sum array function
    Array.prototype.SumArray = function (arr) {
        var sum = [];
        if (arr != null && this.length == arr.length) {
            for (var i = 0; i < arr.length; i++) {
                sum.push(this[i] + arr[i]);`enter code here`
            }
        }
        return sum;
    }
    
    C = A.SumArray(B);
    console.log (C)

Method 2


    var C = A.map(function (num, idx) {
      return num + B[idx];
    });

But the console result is the same as below:


    [
    '25.80.00387000',
    '270.00472000',
    '24.0999999999999980.00534000',
    '30.0700000000000040.00460000',
    '34.30.00060000',
    '34.3000000000000040.00032000',
    '34.30.00053000',
    '330.00327000',
    '29.20.00217000',
    '29.20.00217000',
    '27.60.00460000',
    '28.9999999999999960.00415000',
    '29.3100000000000020.00852000',
    '27.0000000000000040.02241000'
    ]

How can I add two array to make new Array like this:

[ '25.80387000', '27.00472000', . . . . . . ]

Could anyone help to let me know why and how to fix this?

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

The array B is consists of strings, so in order to add it to floats in the other array you need to parse it as a float.

 var C = A.map(function (num, idx) {
      return num + parseFloat(B[idx]);
    });
about 4 years ago · Santiago Trujillo Denunciar

0

This is because the data type to elements in the B array are string, so '+' operation concatenate two elements. you should change the type of the elements in B array to number and then + them.

 var C = A.map(function (num, idx) {
      return num + parseFloat(B[idx]);
    });

this should do by parsing the string to float.

about 4 years ago · Santiago Trujillo Denunciar

0

Not fully sure I understand the question but it seems like you're using strings and numbers interchangeably here. When strings are added to numbers the result is a string.

Look into using the Number wrapper or the parseFloat method depending on what suits your need.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

about 4 years ago · Santiago Trujillo 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