- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2022 12:26 AM
hii,
I need a logic for array ,if we take 12 months values in array how can we calculate the average for each month. Please tell me a scripts for finding the average.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2022 02:11 AM
That's the full script. I've executed the script in Script Background to get the Execution result.
I've added comments in script.
var values = [[1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,33], [4,53,6],[14,2,3], [41,15,6],]; // array of array to process
// prototype to sum array
Array.prototype.sum = Array.prototype.sum || function (){
return this.reduce(function(p,c){return p+c},0); // sum values of elements in an array
};
// prototype to find average of an array
Array.prototype.avg = Array.prototype.avg || function () {
return this.sum()/this.length; // find the sum of elements in an array and divide by number of elements
};
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; // this is just to output name of months
for (var i=0; i< values.length; i++) { // loop through array of array "values"
gs.info(months[i] + ' average is ' + values[i].avg()); // output name of month and average. values[i] is an array item in "values". Calling "avg" prototype defined above.
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2022 01:13 AM
If months are going to have different values interdependent of month values, something like below. Array "monthValues" contains value of each month.
Array.prototype.sum = Array.prototype.sum || function (){
return this.reduce(function(p,c){return p+c},0);
};
Array.prototype.avg = Array.prototype.avg || function () {
return this.sum()/this.length;
};
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var monthValues = [1,2,3,4,5,6,7,8,9,10,11,12]; // array with values of each month
var values = [];
for (var i=0; i< 12; i++) {
values.push(monthValues[i]);
//gs.info(values);
gs.info(months[i] + ' average is ' + values.avg());
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2022 12:56 AM
Something like below to calculate average of each month.
I've created an array of array. Each inner array is a value of each month.
var values = [[1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,33], [4,53,6],[14,2,3], [41,15,6],];
Array.prototype.sum = Array.prototype.sum || function (){
return this.reduce(function(p,c){return p+c},0);
};
Array.prototype.avg = Array.prototype.avg || function () {
return this.sum()/this.length;
};
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
for (var i=0; i< values.length; i++) {
gs.info(months[i] + ' average is ' + values[i].avg());
}
Execution result:
*** Script: Jan average is 2
*** Script: Feb average is 5
*** Script: Mar average is 2
*** Script: Apr average is 5
*** Script: May average is 2
*** Script: Jun average is 5
*** Script: Jul average is 2
*** Script: Aug average is 5
*** Script: Sep average is 12
*** Script: Oct average is 21
*** Script: Nov average is 6.333333333333333
*** Script: Dec average is 20.666666666666668
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2022 01:15 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2022 02:11 AM
That's the full script. I've executed the script in Script Background to get the Execution result.
I've added comments in script.
var values = [[1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,3], [4,5,6],[1,2,33], [4,53,6],[14,2,3], [41,15,6],]; // array of array to process
// prototype to sum array
Array.prototype.sum = Array.prototype.sum || function (){
return this.reduce(function(p,c){return p+c},0); // sum values of elements in an array
};
// prototype to find average of an array
Array.prototype.avg = Array.prototype.avg || function () {
return this.sum()/this.length; // find the sum of elements in an array and divide by number of elements
};
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; // this is just to output name of months
for (var i=0; i< values.length; i++) { // loop through array of array "values"
gs.info(months[i] + ' average is ' + values[i].avg()); // output name of month and average. values[i] is an array item in "values". Calling "avg" prototype defined above.
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2022 06:00 AM
hi Hitoshi Ozawa,
I want the script for the following requirement.
We have a dashboard baseline input table.
In screenshot you can view the column names.The requiremnt is -to calculate the moving average of each months.
Based on any single account :"ABC", onsite and year there are different Median Rates for each month.So,we need to calculate the average of them on month basis.Such as:for January median rate=2,feb =3,april = 4 ,then moving average will be for january=(2/1)=2,feb=(2+3)/2=2.5,april=(2+3+4)/3=3 and for march there are no records ,then moving average for march will be =same as feb that is 2.5.
Just like this for account "ABC" for offshore another records will be there..so moving average will be different for them and also when year changes moving average will also changes based on the records.Refer the below screenshot
Please help me for that with the scripts as soon as posible.
Thanks and Regards,
Sengeni.D