# Creating a for loop for variables in a single POST

**URL:** <https://community.postman.com/t/creating-a-for-loop-for-variables-in-a-single-post/10328>\
**Category:** Help Hub\
**Created:** [January 24, 2020, 4:52pm UTC](https://community.postman.com/t/creating-a-for-loop-for-variables-in-a-single-post/10328 "2020-01-24T16:52:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dleenhouts](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dleenhouts](https://community.postman.com/u/dleenhouts)\
**Post date:** [January 24, 2020, 4:52pm UTC](https://community.postman.com/t/creating-a-for-loop-for-variables-in-a-single-post/10328/1 "2020-01-24T16:52:02Z")

</div>

I’ve got the following pre-request script:

```
    var moment = require('moment');
    pm.environment.set('time0', moment().add(0, 'minutes').toISOString());
    pm.environment.set('time1', moment().add(1, 'minutes').toISOString());
    pm.environment.set('time2', moment().add(2, 'minutes').toISOString());
    pm.environment.set('time3', moment().add(3, 'minutes').toISOString());
    pm.environment.set('time4', moment().add(4, 'minutes').toISOString());
    pm.environment.set('time5', moment().add(5, 'minutes').toISOString());
    pm.environment.set('time6', moment().add(6, 'minutes').toISOString());
    pm.environment.set('time7', moment().add(7, 'minutes').toISOString());
    pm.environment.set('time8', moment().add(8, 'minutes').toISOString());
    pm.environment.set('time9', moment().add(9, 'minutes').toISOString());
    pm.environment.set('time10', moment().add(10, 'minutes').toISOString());

```

And the Body references those timeX variables:

```
 {
    "id": "ID1",
    "timestamp": "{{time0}}",
    "value": 10
  },
  {
    "id": "ID1",
    "timestamp": "{{time1}}",
    "value": 12
  },
  {
    "id": "ID1",
    "timestamp": "{{time2}}",
    "value": 10
  },
    {
    "id": "ID1",
    "timestamp": "{{time3}}",
    "value": 8
  },

```

I’m unsure how to turn this into a for loop that fills in the corresponding time variables in a single POST. It works as-is, but I’m pretty sure there’s a way to reduce the 70 time variables (from now-10 minutes to now+60 minutes) in the pre-request script to a loop. Anyone know how this can be done? Thanks.

---

<div class="post-metadata">

**Author:** ![john-paul](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/john-paul/32/919_2.png) [@john-paul](https://community.postman.com/u/john-paul)\
**Post date:** [January 25, 2020, 12:18am UTC](https://community.postman.com/t/creating-a-for-loop-for-variables-in-a-single-post/10328/2 "2020-01-25T00:18:46Z")

</div>

Hi @dleenhouts! Try this:

```auto
for (var i = 0; i < 11; i++) { // change to i < 71 if you need 70 variables
    pm.environment.set('time'+i,moment().add(i, 'minutes').toISOString());
}

```

---

<div class="post-metadata">

**Author:** ![dleenhouts](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dleenhouts](https://community.postman.com/u/dleenhouts)\
**Post date:** [January 25, 2020, 7:13pm UTC](https://community.postman.com/t/creating-a-for-loop-for-variables-in-a-single-post/10328/3 "2020-01-25T19:13:03Z")

</div>

Thanks, that’s good enough for the now to now+60 part. I tried (var i = -10; i \< 61; i++) but I don’t think you can do something like .add(-10, ‘minutes’). Using a second loop with (var i = 0; i \< 10; i++) & .subtract(i, ‘minutes’) worked for the now-10 to now variables.

---

<div class="post-metadata">

**Author:** ![john-paul](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/john-paul/32/919_2.png) [@john-paul](https://community.postman.com/u/john-paul)\
**Post date:** [January 27, 2020, 5:32pm UTC](https://community.postman.com/t/creating-a-for-loop-for-variables-in-a-single-post/10328/4 "2020-01-27T17:32:45Z")

</div>

@dleenhouts cool! Glad that helped!

Another approach would be declaring a starting timestamp that is -10 minutes from the current time, and adding to that each loop. That would allow you to use a single loop.

---

<div class="post-metadata">

**Author:** ![dleenhouts](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dleenhouts](https://community.postman.com/u/dleenhouts)\
**Post date:** [February 10, 2020, 4:20pm UTC](https://community.postman.com/t/creating-a-for-loop-for-variables-in-a-single-post/10328/5 "2020-02-10T16:20:37Z")

</div>

Is there a way to trim down this request to have a single object like:

```
{
    "id": "ID1",
    "timestamp": "{{time}}",
    "value": 10
}

```

With a loop that sets the {{time}} variable from now-10 minutes to now+60 minutes, rather than having a body with 70 variables? The loop I’m trying to use only sets the {{time}} variable to the last value (71) rather than every value in the loop:

```
for (var i = 0; i < 71; i++) { 
    pm.environment.set('time'+i,moment().add(i, 'minutes').toISOString());
}
console.log(i);
```

---

<div class="post-metadata">

**Author:** ![john-paul](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/john-paul/32/919_2.png) [@john-paul](https://community.postman.com/u/john-paul)\
**Post date:** [February 10, 2020, 11:22pm UTC](https://community.postman.com/t/creating-a-for-loop-for-variables-in-a-single-post/10328/6 "2020-02-10T23:22:06Z")

</div>

Whoops, yeah that’s my bad. You’ll need to loop the request using postman.setNextRequest(), and store the value of i in the environment. Let me throw a working example of this together, and I’ll share shortly.

---

<div class="post-metadata">

**Author:** ![john-paul](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/john-paul/32/919_2.png) [@john-paul](https://community.postman.com/u/john-paul)\
**Post date:** [February 11, 2020, 12:13am UTC](https://community.postman.com/t/creating-a-for-loop-for-variables-in-a-single-post/10328/7 "2020-02-11T00:13:05Z")

</div>

@dleenhouts Sorry again about that. Click Run In Postman on the following docs page to get a working example of how to do this:

[https://documenter.getpostman.com/view/5293839/SWTK4EWu](https://documenter.getpostman.com/view/5293839/SWTK4EWu)

---

<div class="post-metadata">

**Author:** ![dleenhouts](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dleenhouts](https://community.postman.com/u/dleenhouts)\
**Post date:** [February 11, 2020, 1:52am UTC](https://community.postman.com/t/creating-a-for-loop-for-variables-in-a-single-post/10328/8 "2020-02-11T01:52:21Z")

</div>

Thanks, think I got it.

BTW I’m trying this reply via email since I cannot log into the Postman community website (click Login, select Google account, just redirects back to this topic. Tried clearing browsing data, different browser, different machine. Tried “trouble signing in” + sending pw reset email, never got the reset email). Any suggestions there?

---

<div class="post-metadata">

**Author:** ![john-paul](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/john-paul/32/919_2.png) [@john-paul](https://community.postman.com/u/john-paul)\
**Post date:** [February 11, 2020, 8:13pm UTC](https://community.postman.com/t/creating-a-for-loop-for-variables-in-a-single-post/10328/9 "2020-02-11T20:13:37Z")

</div>

@dleenhouts we’re currently tracking this issue and working on a fix. Hope to have it addressed soon.
