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

203
Views
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 answers
Answer question

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 Report

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 Report

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 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!