Home > Net >  sum object from multi arrays in app script
sum object from multi arrays in app script

Time:09-27

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

CodePudding user response:

numbersInArray = price.flat()

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related