2

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

johnvan
  • 75
  • 1
  • 7

4 Answers4

2

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

Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23
  • Some time Give me result like that: `1.5295400000000001` or `16643.59000000003` , can I split result but after dot, the simple way beginning from first number `.slice(0,5)` , I want to see like that : `1.5295` or `16643.5900` – johnvan Sep 26 '21 at 19:55
  • I've added the common trick to limit max number of figures after dot. It will give you `16643.59` instead `16643.59000000003`. But if you want zeros at the end (`16643.5900`) it will need another implementation. – Yuri Khristich Sep 26 '21 at 20:08
  • I've added another implementation with zeros at the end. – Yuri Khristich Sep 26 '21 at 20:13
  • The last formula work great thanks brother for help – johnvan Sep 26 '21 at 20:20
  • The first implementation is a classic algorithm via calculation. If you need 8 digits you can multiply/divide your number by 1 00 000 000 (8 zeros). The second implementation is string manipulations: you convert your number into string, split at the dot, cut redundant digits from the end and concatenate the string back. – Yuri Khristich Sep 26 '21 at 20:27
  • I use this `const dot1000 = x => parseInt(x) + '.' + (x+.00001+'').split('.')[1].slice(0,4)` and work great. I just change `.slice(0,4)` to `.slice(0,8)` to get 8 number after DOTE. The result like that : `966843.48632100` any solution to convert to number to add comma between numbers to read easy like that `966,843.48632100` – johnvan Sep 28 '21 at 20:06
  • It's a quite popular question there are many solutions see here: https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – Yuri Khristich Sep 29 '21 at 07:23
0

You are adding the array in which each number (both obj[0] and obj[1] values) is treated as String type. So that's why they are not added like numbers but concatenated. First convert the String Type into Number. Then your problem will be resolved.

As I don't know about the used API. So I could not give answer with API response included. But I am giving you code where I do some changes but just look at the praseFloat() method that I used.

const Api_Response_Array = [["30.56", "1.014"], ["50.44", "1.019"], ["10.35", "1.081"], ["10.34", "1.115"], ["10.40", "2.006"]];
var first= 0;
var second= 0;

Api_Response_Array.forEach(function(){
   first+= parseFloat(Api_Response_Array[0][0]);
   second+= parseFloat(Api_Response_Array[0][1]);
});
document.write(first + "<br>");
document.write(second);

Just use praseFloat method inside function used within forEach loop. It will convert the string into number.

Matthias Steinbauer
  • 1,786
  • 11
  • 24
Developer
  • 1,297
  • 1
  • 4
  • 15
0

numbersInArray = price.flat()

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

Neven Subotic
  • 1,399
  • 1
  • 6
  • 18
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

ABDULLOKH MUKHAMMADJONOV
  • 4,249
  • 3
  • 22
  • 40