Perform math on two arrays, saving results as 3rd array which I can iterate against w/ runner

I have

Array 1 = Credit Balance

Array 2 = Credit Limit

Arrays are created as mapped values from API response:

const response = pm.response.json();

const E_Mail = response.value.map(value => value.E_Mail);

const Balance_LCY = response.value.map(value => value.Balance_LCY);

const Credit_Limit_LCY = response.value.map(value => value.Credit_Limit_LCY);

console.log(“Email Is:”,E_Mail,“Balance Is:”,Balance_LCY,“Credit Limit Is:”,Credit_Limit_LCY);

let a = parseInt(pm.environment.get('Balance_LCY'))

let b = parseInt(pm.environment.get('Credit_Limit_LCY'))

availcredit = b =- a

let c = availcredit

pm.environment.set('E_Mail', E_Mail);

pm.environment.set('Balance_LCY', Balance_LCY);

pm.environment.set('Credit_Limit_LCY', Credit_Limit_LCY);

pm.environment.set('availcredit',c)

console.log(“Credit Limit Minus Balance:”,c);

========================

Array 1: 641.5,0,2490.17,0,1057.68,0,-1733,0,-1201.58,0,0,0,2061.6,0,1044.12,69631.05,0,0,2000.92,127.44,0,0,0,0,0,0,0,0,0,3074.22,0.7,0,0,435,-464.1,0,0,0,0,0,0,259,-117.17,0,0,0,0,0,9090,16459.2,-408.76,19761.1,0,0,-2058.9,0,100347.03,0,3498.29,12696.02,4341.46,0,2466.98,0,4047.32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24669.53,0,-4533.19,0,1490.73,-994.14,0,0,0,0,0,0,0,0,0,8053.66,6887.45,0,5574.67,6315.12,-1510.03,0,30413.36,0,27558.35,0,2384.31,0,27438.96,42500.05,35289.81,0,0,3180.06,0,0,0,0,0,0,0,0,0,8993.05,31763.61,6661.71,4827.39,10019.2,0,0,1365994.77,24269,0,0,0,0,0,14961.05,0,0,0,0,0,4728.17,0,0,1251.6,0,0,15790.6,0,0,500,0,3339.75,4651.67,0,0,23804.33,0,14723.16,0,1868.4,0,0,11322.43,2418.14,-5072.42,11346.34,0,0,60071.07,0,0,5980,5643.48,8755.66,5274.26,0,9265.11,24853.95,0,-1368.72,-4213.85,70902.62,0,2821.92,4837.28,0,3026.05,7061.06,22884.12,0,0,1584,-184.09,12068.1,0,0,0,-414.72,0,0,13537.05,0,0,898.56,-2,273.84,0,0,139.98,-192.96,0,0,0,0,0,0,0,0,-7713.45,70235.59,47715.62,0,0,0,0,0,0,0,20942,0,0,0,0,0,0,0,0,2051.26,0,0,10296.06,0,0,0,0,0,0,0,16750.8,2791.8,2791.8,2791.8,0,0,0,0,3475.8,2568.15,3505.08,0,0,0,0,0,13112,0,0,0,0,33827.69,0,2028.85,0,0,0,0,0,0,0,0,0,15075.43,0,0,0,0,0,8328.18,0,1576.12,0,0,10483.2,0,0,0,0,0,-16.08,8768.28,0,9459.81,30602.26,-173.12,0,0,0,0,0,0,0,13525.46,0,0,10566.52,0,8099.45,0,3771,0,0,0,0,-76.89,3952.11,0,0,6702.4,0,0,5818.79,4023.18,0,0,9359.68,41750.28,0,0,0,0,0,0,0,0,0,0,5295.28,0,0,0,0,0,0,7180.22,0

Array 2: 500,500,1000,1000,1500,500,1000,1000,500,1000,500,1000,500,500,500,50000,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,0,7000,100000,3000,40000,10000,15000,5000,55000,125000,15000,50000,10000,20000,10000,15000,15000,3500,25000,25000,10000,25000,25000,25000,25000,25000,25000,25000,25000,25000,25000,3000,10000,50000,10000,15000,3000,4000,3500,10000,50000,50000,50000,50000,50000,50000,50000,50000,5000,9000,6000,10000,30000,20000,50000,50000,7500,20000,20000,5000,100000,15000,7000,20000,25000,10000,2000,5000,5000,5000,5000,5000,5000,5000,500000,5000,40000,45000,20000,1000,5000,5000,10000,2800000,65000,50000,50000,50000,50000,50000,20000,10000,5000,5000,5000,5000,5000,7500,7000,5000,6000,6000,6000,2000,5000,10000,5000,3000,10000,5000,25000,12000,7500,10000,4000,4500,20000,3500,2500,3500,2500,5000,8000,20000,40000,2000,20000,35000,5000,50000,5500,10000,3000,35000,20000,5000,3000,9000,10000,0,15000,10000,3000,10000,15000,10000,10000,5500,100000,7000,50000,1600,300,25000,1000,2000,0,0,0,5000,2500,5000,5000,1500,5000,10000,10000,5000,60000,2000,50000,1000,200000,25000,25000,25000,0,30000,350,10000,5000,10000,15000,5000,20000,3000,8000,50000,5000,5000,10000,15000,10000,10000,6000,10000,5000,10000,5500,20000,20000,10000,10000,1250,20000,20000,20000,20000,5000,20000,10000,0,10000,10000,20000,10000,10000,10000,10000,10000,1500,10000,10000,10000,10000,15000,2500,10000,4000,10000,14000,10000,4000,7000,7000,7000,10000,30000,40000,10000,4000,10000,10000,10000,3000,4000,10000,7000,10000,8000,5000,10000,3000,10000,25000,6000,3000,10000,12000,15000,10000,15000,7500,50000,10000,500,5000,4000,500,8000,88000,10000,4000,10000,4000,6000,8000,5000,7500,0,4000,8000,7000,9000,7000,50000,22000,4000,0,10000,8000,40000,8000,8000,5000,8000,8000,4000,10000,10000,20000,8000,6000,5000,10000,20000,7000,20000,20000,5000,100000

This gives me availcredit = -641

That is value of array 1, position 1, but as a negative.

I really cannot figure this out.

I want to subtract array1.position1 from array2.position1 saving as array3.position1
Subtract array1.position2 from array2.position2 saving as array3.position2, and so on.

My goal is to iterate against this array, shifting 1 using pre-request script in my 2nd request which is:

//WORKING CREDIT APP PRE TEST

//Setting constant E_Mail from variable derived from “Get CustomerList”

const E_Mail = pm.variables.get(‘E_Mail’);

//Setting constant Balance_LCY from variable derived from “Get CustomerList”

const Balance_LCY = pm.variables.get(‘Balance_LCY’);

//Setting variable ‘Current_E_Mail’, selecting 1 value, then shifting offset +1

pm.variables.set(‘Current_E_Mail’, E_Mail.shift());

if (Array.isArray(E_Mail) && E_Mail.length > 0) {

postman.setNextRequest("Adjust Credit")

//Setting variable ‘Current_Balance’, selecting 1 value, then shifting offset +1

pm.variables.set(‘Current_Balance’, Balance_LCY.shift())}

//Setting next request to “Adjust Credit” forming loop w/ +1 from previous command

else {

postman.setNextRequest(“null”)}

Please – any help is appreciated.

I previously was iterating against “Balance_LCY”

My goal is to change pre-request script of request 2 to
pm.variables.set(‘Current_Balance’, availcredit.shift())}

I saw a mistake I made - I updated my code.

console.log(“Email Is:”,E_Mail,“Balance Is:”,Balance_LCY,“Credit Limit Is:”,Credit_Limit_LCY);

let a = parseInt(pm.environment.get('Balance_LCY'))

let b = parseInt(pm.environment.get('Credit_Limit_LCY'))

availcredit = b - a

let c = availcredit

pm.environment.set('E_Mail', E_Mail);

pm.environment.set('Balance_LCY', a);

pm.environment.set('Credit_Limit_LCY', b);

pm.environment.set('availcredit',c)

console.log(“Credit Limit Minus Balance:”,c);

This returns availcredit = -1282

Considering array 1 position 1 = 641.5
array 2 position 2 = 500

b-a should = -141.5, but I am getting -1282

I got it to properly calculate the 1st value w/ the following:

var lodash = require(‘lodash’);
var arr = Balance_LCY;
var sum = lodash.sum(arr);
console.log(sum);

console.log(“Credit Limit Minus Balance:”,c);

pm.test(“Response 200 True/False | Pass/Fail”, function () {
pm.response.to.have.status(200);});

This is now passing -140 as the ‘availcredit’

I just want it to do this for all values, and store as an array

Hello @descent-module-saga4, sorry to say, but I was not able to clearly interpret your above code. But as per your requirement, you can use the below code.

This code performs math and saves the result in new array.

const arr1 = [641.5,0,2490.17,0,1057.68,0,-1733,0,-1201.58,0,0,0,2061.6,0,1044.12,69631.05,0,0,2000.92,127.44,0,0,0,0,0,0,0,0,0,3074.22,0.7,0,0,435,-464.1,0,0,0,0,0,0,259,-117.17,0,0,0,0,0,9090,16459.2,-408.76,19761.1,0,0,-2058.9,0,100347.03,0,3498.29,12696.02,4341.46,0,2466.98,0,4047.32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24669.53,0,-4533.19,0,1490.73,-994.14,0,0,0,0,0,0,0,0,0,8053.66,6887.45,0,5574.67,6315.12,-1510.03,0,30413.36,0,27558.35,0,2384.31,0,27438.96,42500.05,35289.81,0,0,3180.06,0,0,0,0,0,0,0,0,0,8993.05,31763.61,6661.71,4827.39,10019.2,0,0,1365994.77,24269,0,0,0,0,0,14961.05,0,0,0,0,0,4728.17,0,0,1251.6,0,0,15790.6,0,0,500,0,3339.75,4651.67,0,0,23804.33,0,14723.16,0,1868.4,0,0,11322.43,2418.14,-5072.42,11346.34,0,0,60071.07,0,0,5980,5643.48,8755.66,5274.26,0,9265.11,24853.95,0,-1368.72,-4213.85,70902.62,0,2821.92,4837.28,0,3026.05,7061.06,22884.12,0,0,1584,-184.09,12068.1,0,0,0,-414.72,0,0,13537.05,0,0,898.56,-2,273.84,0,0,139.98,-192.96,0,0,0,0,0,0,0,0,-7713.45,70235.59,47715.62,0,0,0,0,0,0,0,20942,0,0,0,0,0,0,0,0,2051.26,0,0,10296.06,0,0,0,0,0,0,0,16750.8,2791.8,2791.8,2791.8,0,0,0,0,3475.8,2568.15,3505.08,0,0,0,0,0,13112,0,0,0,0,33827.69,0,2028.85,0,0,0,0,0,0,0,0,0,15075.43,0,0,0,0,0,8328.18,0,1576.12,0,0,10483.2,0,0,0,0,0,-16.08,8768.28,0,9459.81,30602.26,-173.12,0,0,0,0,0,0,0,13525.46,0,0,10566.52,0,8099.45,0,3771,0,0,0,0,-76.89,3952.11,0,0,6702.4,0,0,5818.79,4023.18,0,0,9359.68,41750.28,0,0,0,0,0,0,0,0,0,0,5295.28,0,0,0,0,0,0,7180.22,0];
const arr2 = [500,500,1000,1000,1500,500,1000,1000,500,1000,500,1000,500,500,500,50000,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,0,7000,100000,3000,40000,10000,15000,5000,55000,125000,15000,50000,10000,20000,10000,15000,15000,3500,25000,25000,10000,25000,25000,25000,25000,25000,25000,25000,25000,25000,25000,3000,10000,50000,10000,15000,3000,4000,3500,10000,50000,50000,50000,50000,50000,50000,50000,50000,5000,9000,6000,10000,30000,20000,50000,50000,7500,20000,20000,5000,100000,15000,7000,20000,25000,10000,2000,5000,5000,5000,5000,5000,5000,5000,500000,5000,40000,45000,20000,1000,5000,5000,10000,2800000,65000,50000,50000,50000,50000,50000,20000,10000,5000,5000,5000,5000,5000,7500,7000,5000,6000,6000,6000,2000,5000,10000,5000,3000,10000,5000,25000,12000,7500,10000,4000,4500,20000,3500,2500,3500,2500,5000,8000,20000,40000,2000,20000,35000,5000,50000,5500,10000,3000,35000,20000,5000,3000,9000,10000,0,15000,10000,3000,10000,15000,10000,10000,5500,100000,7000,50000,1600,300,25000,1000,2000,0,0,0,5000,2500,5000,5000,1500,5000,10000,10000,5000,60000,2000,50000,1000,200000,25000,25000,25000,0,30000,350,10000,5000,10000,15000,5000,20000,3000,8000,50000,5000,5000,10000,15000,10000,10000,6000,10000,5000,10000,5500,20000,20000,10000,10000,1250,20000,20000,20000,20000,5000,20000,10000,0,10000,10000,20000,10000,10000,10000,10000,10000,1500,10000,10000,10000,10000,15000,2500,10000,4000,10000,14000,10000,4000,7000,7000,7000,10000,30000,40000,10000,4000,10000,10000,10000,3000,4000,10000,7000,10000,8000,5000,10000,3000,10000,25000,6000,3000,10000,12000,15000,10000,15000,7500,50000,10000,500,5000,4000,500,8000,88000,10000,4000,10000,4000,6000,8000,5000,7500,0,4000,8000,7000,9000,7000,50000,22000,4000,0,10000,8000,40000,8000,8000,5000,8000,8000,4000,10000,10000,20000,8000,6000,5000,10000,20000,7000,20000,20000,5000,100000];
const arrDifference = (arr1, arr2) => {
   const result = [];
   for(let i = 0; i < arr1.length; i++){
      const subVal = Math.abs((arr1[i] || 0) - (arr2[i] || 0));
      result[i] = subVal;
   };
   return result;
};
console.log(arrDifference(arr1, arr2));

I hope this helps you. Code can be polished more towards your requirement.

Cheers :cake:

Hey there, thank you for your reply.

I ended up getting this to work by finding a tutorial for exactly that code here:

How to subtract elements of two arrays and store the result as a positive array in JavaScript? (verbatim script)

My script is as follows:

const absDifference = (arr2, arr1) => {

const res = [];

for(let i = 0; i < arr2.length; i++){

    const el = Math.ceil((arr2[1]) - (arr1[i]));

    res[i] = el;

}

return res;

};

Available_Credit = absDifference(arr2,arr1);

console.log(“ArrayDifference”,absDifference(arr2,arr1));

You actually solved my problem!!!

For this line:
const el = Math.ceil((arr2[1]) - (arr1[i]));

I updated to:
const el = Math.ceil((arr2[i] || 0) - (arr1[i] || 0));

And now the math actually gives the difference!!!

I use math.ceil because I have negative values I must retain. Ceil will round to nearest value, removing floating point. Since this is a margin of less than 1 dollar, I am happy with ceil results. Unfortunately absolutes wouldn’t work, and data structure of all arrays needed to retain order for automation to function.

This is now working precisely how I want it to!

For anyone who is curious, the following is my full code:

pm.test(“Gather Email, Balance, Credit Limit. Parse A=Balance LCY | B=Credit Limit LCY | b - a = availcredit”);

const response = pm.response.json();

const E_Mail = response.value.map(value => value.E_Mail);

const Balance_LCY = response.value.map(value => value.Balance_LCY);

const Credit_Limit_LCY = response.value.map(value => value.Credit_Limit_LCY);

console.log(“Email Is:”,E_Mail,“Balance Is:”,Balance_LCY,“Credit Limit Is:”,Credit_Limit_LCY);

let a = parseInt(pm.environment.get('Balance_LCY'));

let b = parseInt(pm.environment.get('Credit_Limit_LCY'));

availcredit = b-a;

let c = availcredit;

pm.environment.set('E_Mail', E_Mail);

pm.environment.set('Balance_LCY', Balance_LCY);

pm.environment.set('Credit_Limit_LCY', Credit_Limit_LCY);

pm.environment.set('availcredit',c);

console.log(“Credit Limit Minus Balance:”,c);

pm.test("Response 200 True/False | Pass/Fail", function () {

pm.response.to.have.status(200);});

pm.test(“Array Math Test”);

const arr1 = Balance_LCY;

const arr2 = Credit_Limit_LCY;

const absDifference = (arr2, arr1) => {

const res = [];

for(let i = 0; i < arr2.length; i++){

    const el = Math.ceil((arr2[i] || 0) - (arr1[i] || 0));

    res[i] = el;

}

return res;

};

Available_Credit = absDifference(arr2,arr1);

console.log(“ArrayDifference”,absDifference(arr2,arr1));

pm.environment.set(“Array3”,Available_Credit)