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

2
  • 1
    first is an array, not a number. Commented Sep 26, 2021 at 18:28
  • yeh I know just do that for test Commented Sep 26, 2021 at 18:44

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

7 Comments

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
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.
I've added another implementation with zeros at the end.
The last formula work great thanks brother for help
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.
|
0

numbersInArray = price.flat()

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

Comments

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

Comments

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.

2 Comments

I try but not working can you write it by code to help all social people
Oky I edit my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.