Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

calculate avg count from month in year js

let arr = [
  [ 2020, 1, 2, 1058 ],
  [ 2020, 1, 8, 1055 ],
  [ 2020, 1, 12, 1058 ],
  [ 2020, 1, 23, 1049 ],
  [ 2020, 1, 24, 1050 ],
  [ 2020, 1, 29, 1057 ],
  [ 2020, 2, 1, 1088 ],
  [ 2020, 2, 5, 1087 ],
  [ 2020, 2, 13, 1101 ],
  [ 2020, 2, 26, 1108 ],
  [ 2020, 4, 25, 1119 ],
  [ 2020, 8, 24, 1178 ],
  [ 2020, 8, 25, 1196 ],
  [ 2020, 9, 29, 1214 ],
  [ 2020, 9, 31, 1230 ],
  [ 2020, 10, 20, 1259 ],
  [ 2020, 11, 18, 1276 ]
];
let result = [];
let result2 = arr.reduce((res, [year, month, day, value]) => {
  if (!res[month]) {
    res[month] = { month: month, avg: 0, count: 0, sum: 0 };
    result.push(res[month])//remove this if you are satisfied with the result in result2;
  }
  res[month].sum += value;
  res[month].count +=1;
  res[month].avg = res[month].sum/res[month].count;
  return res;
} , {});
console.log('array of objects {[month, sum, count, avg]}', result);
console.log('object with values {month: {month, sum, count, avg}}', result2);
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular auth guard @medium 
Javascript :: ajaxpost 
Javascript :: javascript get css property 
Javascript :: How to Solve the Staircase Problem with 5 Lines of JavaScript 
Javascript :: Simplest Template Example 
Javascript :: how to read json data from database in laravel 
Javascript :: netsuite get search column value suitescript 
Javascript :: get data from json key with special character 
Javascript :: telerik grid destroy table 
Javascript :: js calculate hours between two times 
Javascript :: Component With Both Data And Props 
Javascript :: passing third parameter in context.commit vuejs 
Javascript :: how to make react host on https localhost 
Javascript :: nextjs on route change content not changing 
Javascript :: NodeJS Multi-Core Processors Example 
Javascript :: Search an elemnt in a sorted and rotated array 
Javascript :: javascript array every 
Javascript :: how to check vowels in a string in javascript 
Javascript :: symfony iterate over entity 
Javascript :: html select structure 
Javascript :: how to convert javascript to typescript angular 
Javascript :: how to get the first element in an array in javascript 
Javascript :: sort used in price high and low using angular 
Javascript :: uncheck all other checkboxes when one is checked 
Javascript :: create index with multiple fields mongo 
Javascript :: javascript How to show array content in output window 
Javascript :: angularjs How to set code view as deafult instead of tree in jsoneditor 
Javascript :: Issue in applying margin using angular "data-ng-style" 
Javascript :: React Native Root Element, deciding on async call 
Javascript :: supertest npm send headers node js 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =