# Sum of collection variables

**URL:** <https://community.postman.com/t/sum-of-collection-variables/46273>\
**Category:** Help Hub\
**Tags:** variables, pre-request-script\
**Created:** [April 12, 2023, 9:06pm UTC](https://community.postman.com/t/sum-of-collection-variables/46273 "2023-04-12T21:06:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brcox9090](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@brcox9090](https://community.postman.com/u/brcox9090)\
**Post date:** [April 12, 2023, 9:06pm UTC](https://community.postman.com/t/sum-of-collection-variables/46273/1 "2023-04-12T21:06:34Z")

</div>

So, I have a few variable set in a collection that are numbers:

 ![Screenshot 2023-04-12 at 5.03.00 PM](https://us1.discourse-cdn.com/getpostman/original/3X/0/4/04a8d09eceb9518a35c20d274486f3e9463f8c2a.png)

And I’m looking to run a call that adds these variables up, and sets the sum of them as “alltotal”.

I’ve tried things like:

let allcount = +sub1total + +sub2total (etc.) – I get an error stating sub1total isn’t set.

I’ve tried to parse these into integers using:  
console.log(parseInt(pm.environment.get(“sub1total”)) + parseInt(pm.environment.get(“sub2total”))) – get the same error.

Not sure where to go.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [April 13, 2023, 8:23am UTC](https://community.postman.com/t/sum-of-collection-variables/46273/2 "2023-04-13T08:23:06Z")

</div>

Hi @brcox9090

You could do something like:

```auto
//Get all vars from environment
let subsub1 = parseInt(pm.environment.get('subsub1'));
let sub1total = parseInt(pm.environment.get('sub1total'));
let subsub2 = parseInt(pm.environment.get('subsub2'));
let sub2total = parseInt(pm.environment.get('sub2total'));
let sub3total = parseInt(pm.environment.get('sub3total'));
let sub22 = parseInt(pm.environment.get('sub22'));

//add them
let alltotal = subsub1 + sub1total + subsub2 + sub2total + sub3total + sub22;

//output to console and send total to environment
console.log(alltotal);
pm.environment.set('alltotal', alltotal)

```

* * *

Output:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/f/e/fe19b0e91f6eadb4415dcf1de2d7ffbe16cadce7.png)

![image](https://us1.discourse-cdn.com/getpostman/original/3X/f/9/f9904b084dc1a38ebc015aa80dc16bbc8ef1ba46.png)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 13, 2023, 10:09am UTC](https://community.postman.com/t/sum-of-collection-variables/46273/3 "2023-04-13T10:09:49Z")

</div>

This could work too but it’s only going to work if the only thing you have in your environment file is a set of variables that have numbers:

```auto
let obj = pm.environment.toObject(),
    sumValues = obj => Object.values(obj).reduce((a, b) => parseInt(a) + parseInt(b), 0);

pm.environment.set('sumTotal', sumValues(obj))

```

I totally stole this from a question on SO 😃

> <https://stackoverflow.com/questions/16449295/how-to-sum-the-values-of-a-javascript-object>

---

<div class="post-metadata">

**Author:** ![brcox9090](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@brcox9090](https://community.postman.com/u/brcox9090)\
**Post date:** [April 19, 2023, 4:11pm UTC](https://community.postman.com/t/sum-of-collection-variables/46273/4 "2023-04-19T16:11:28Z")

</div>

Thank you for the response! This got me the expected return. Getting better with this stuff thanks to these answers so I really appreciate it!
