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

93
Views
sum object from multi arrays in app script

I'm using app script

I have Return array from API by this code :

const price= jsonResponce.price.map(obj => [obj[0],obj[1]]);

Give me [[30.56, 1.014], [50.44, 1.019], [10.35, 1.081], [10.34, 1.115], [10.40, 2.006]]

Not this array can be has 1000 array or large

Now I want to sum all object in obj[0] by using this code :

I use to method to see the deference but nothing work

var first= [];
var second= 0;

price.forEach(function(obj){
   first+= obj[0];
   second+= obj[1];
  });
Logger.log(first);
Logger.log(second);

But Give me result like that: first Logger.log(first);

30.5650.4410.3510.3410.40

second Logger.log(second); : this method add number 0 after any obj

01.01401.01901.08101.11502.006

Any idea for this problem

30.56+50.44+10.35+10.34+10.40 I need result as : 112.09

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

0

Your code works fine for me, after minimal corrections:

const price = [[30.56, 1.014], [50.44, 1.019], [10.35, 1.081], [10.34, 1.115], [10.40, 2.006]]

var first  = 0; // <-- here
var second = 0;

price.forEach(obj => {
   first  += +obj[0];  // <-- here
   second += +obj[1];  // <-- here
});

console.log(first);  // --> 112.09
console.log(second); // --> 6.2349

You can get four digits after dot this way:

var a = 1.23456789;
var b = 1234.56789;
var c = 16643.59000000003

const dot1000 = x => Math.round(x*10000)/10000;

console.log(dot1000(a));
console.log(dot1000(b));
console.log(dot1000(c));

Another implementation (with zeros at the end)

var a = 1.23456789
var b = 1234.56789
var c = 16643.59000000003

const dot1000 = x => parseInt(x) + '.' + (x+.00001+'').split('.')[1].slice(0,4)

console.log(dot1000(a))
console.log(dot1000(b))
console.log(dot1000(c))

Update

Modern JavaScript (ES2017) can add zeros this way:

console.log('123'.padEnd(5,'0')); // 12300

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

about 4 years ago · Juan Pablo Isaza Report

0

numbersInArray = price.flat()

let sum = 0 numbersInArray.forEach( number => sum += number)

about 4 years ago · Juan Pablo Isaza Report

0

Using Array.prototype.reduce() method, this becomes very easy:

const result = arr.reduce((total, el)=> {
    return total + el[0] + el[1] // el = [a, b]
}, 0)

console.log(result) /// 118.325

Let me know in the comments if this is what you want or you want any improvements

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!