Logic for array

Sengeni
Tera Contributor

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.

 

1 ACCEPTED SOLUTION

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.
}

View solution in original post

9 REPLIES 9

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());
}

Hitoshi Ozawa
Giga Sage
Giga Sage

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

hii Hitoshi Ozawa,

Thanks for your reply.If u please send me a full script then it will help me to understand better and easy.

Thanks ,

Sengeni.D

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.
}

hi Hitoshi Ozawa,

I want the script for the following requirement.

We have a dashboard baseline input table.image

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

image

Please help me for that with the scripts as soon as posible.

Thanks and Regards,

Sengeni.D