Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get sum of a object in array Javasript and store it

i have this array :

here is a snippet :

[
  {
    "month": 9,
    "date": "2022-09-01T00:00:00",
    "week_nb": 35,
    "total_Value": 31.356,

  },
  {
    "month": 9,
    "date": "2022-09-17T00:00:00",
    "week_nb": 36,
    "total_Value": 67.726,

  },
  {
    "month": 9,
    "date": "2022-09-08T00:00:00",
    "week_nb": 35,
    "total_Value": 98.63,
  },
  {
    "month": 9,
    "date": "2022-09-24T00:00:00",
    "week_nb": 36,
    "total_Value": 55.178,
   
  },
....

i already specify this by month number

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i want to get the sum of TOTALE_VALUE of each WEEK_NB

and store it inside a new array containt 2 objects like

{week_number : X, SumOfTotale_value : X },{week_number : X, SumOfTotale_value : X }...

>Solution :

You could use Array.reduce() to sum the total value grouped by week.

We’d create an object with a property for each week number, then sum for each matching item.

Object.values() can then be used to convert this object to an array as required.

const input = [ { "month": 9, "date": "2022-09-01T00:00:00", "week_nb": 35, "total_Value": 31.356,  }, { "month": 9, "date": "2022-09-17T00:00:00", "week_nb": 36, "total_Value": 67.726,  }, { "month": 9, "date": "2022-09-08T00:00:00", "week_nb": 35, "total_Value": 98.63, }, { "month": 9, "date": "2022-09-24T00:00:00", "week_nb": 36, "total_Value": 55.178,  } ] 

const result = Object.values(input.reduce((acc, { week_nb, total_Value }) => { 
    acc[week_nb] = acc[week_nb] || { week_number: week_nb, SumOfTotale_value: 0 };
    acc[week_nb].SumOfTotale_value += total_Value;
    return acc;
}, {}));

console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; }

You can also use a for..of loop to get the same result:

const input = [ { "month": 9, "date": "2022-09-01T00:00:00", "week_nb": 35, "total_Value": 31.356,  }, { "month": 9, "date": "2022-09-17T00:00:00", "week_nb": 36, "total_Value": 67.726,  }, { "month": 9, "date": "2022-09-08T00:00:00", "week_nb": 35, "total_Value": 98.63, }, { "month": 9, "date": "2022-09-24T00:00:00", "week_nb": 36, "total_Value": 55.178,  } ] 

const map = {};

for(let { week_nb, total_Value } of input) {
    map[week_nb] = map[week_nb] || { week_number: week_nb, SumOfTotale_value: 0 }
    map[week_nb].SumOfTotale_value += total_Value;
}

console.log('Result:', Object.values(map))
.as-console-wrapper { max-height: 100% !important; }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading