Random numbers from array into postman test

As a javascript beginner, I’m struggling a little to get my head around a function I need postman to perform.
I have an array of 15 random numbers, I need to pick 5 numbers from this and send with a postman POST request, but each time I send the number I must include the last number picked ( I.e 1 then 1,2 then 1,2,3, etc)
I know I need to utilise the get next request function in postman to move on when all 5 numbers have been picked.
While I know the javascript I would use to do this if I were just to send this into the console on a browser and have it return the numbers I could use, I have no idea how to put this into the test tabs in postman so I can use the runner and run through the request from start to end and have it move on when complete.

Below is the javascript I have been playing with just for reference:
var myArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; //Setting the array.
var newArray = myArray.sort(() => Math.random() - 0.5); //randomise the array.
let contPicks = myArray.slice(0, 5); //pick first 5 values from the shuffled array
let pickOne = contPicks.slice(0,1);
let pickTwo = contPicks.slice(0,2);
let pickThree = contPicks.slice(0,3);
let pickFour = contPicks.slice(0,4);
let pickFive = contPicks.slice(0,5);

It’s probably not the most efficient way of doing this, as I said, I’m a beginner and while I understand the syntax, applying it is another thing entirely! lol

Hello @Natasha.Brown, welcome to the community! :wave:

Pardon me if I misunderstood you.
So, according to what I got is you have an array of random numbers and while sending the POST request, that array needs to be sent as the part of the request body.

Well,
Here’s an answer that’s 90% close to what you’re trying to achieve.

The answer above is related to using the variable from datafile in collection runner, however you can ignore that and jump ahead.

So instead of this line:

let productLine = pm.iterationData.get('productLine');

you’ll be writing something like:

let productLine = pickOne; // Or whichever array you want to send

You’ll need to just assign your array.

You can tweak around more, by exploring the amazing concept of variables in Postman.

Let me know if you’re still stuck with this!

1 Like