How to calculate a mean by group in a JS Array?

Julien

I work with Angular JS framework and have an array built with NodeJS :

var arr = [{"object":"Anguille","fct":"Type A","value":"2"},{"object":"Athérine","fct":"Type A","value":"1.0031643586595139"}, {"object":"Epinoche","fct":"Type B","value":"1"}, {"object":"Mulet","fct":"Type B","value":"5"}];

I'd like to calculate the mean of "value" grouped by each "fct" so that I could get :

{ 'Type A': 1.5, 'Type B': 3 }

After some researches I managed to group by the values but only with a sum and not a mean. I guess I should use the arr.reduce function but don't really know how to insert it in my code.

Here is my snippet :

const obj = {};
arr.forEach((item) => {
  obj[item.fct] = obj[item.fct] ? obj[item.fct] + 
  parseInt(item.value) : parseInt(item.value);
});

console.log(obj);

I must admit I'm getting stuck. Do you know how to adapt my snippet with a mean instead of a sum ?
Any help would be greatly appreciated, thanks.

Nina Scholz

You could get the totals and counts from every group and build the mean for the result object.

const
    data = [{ object: "Anguille", fct: "Type A", value: "2" }, { object: "Athérine", fct: "Type A", value: "1.0031643586595139" }, { object: "Epinoche", fct: "Type B", value: "1" }, { object: "Mulet", fct: "Type B", value: "5" }],
    grouped = data.reduce((r, { fct, value }) => {
        r[fct] ??= { total: 0, count: 0 };
        r[fct].total += +value;
        r[fct].count++;
        return r;
    }, {}),
    result = Object.fromEntries(Object
        .entries(grouped)
        .map(([key, { total, count }]) => [key, total / count])
    );

console.log(result);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to calculate the mean of a new group?

From Dev

How to calculate mean in group by another group?

From Dev

Group by and calculate mean / average of properties in a Javascript array

From Dev

in R, how to calculate mean of all column, by group?

From Dev

How to calculate mean spatial location by group

From Dev

How to group specific items in a column and calculate the mean

From Dev

Calculate Group Mean and Overall Mean

From Dev

Calculate mean by group with dplyr

From Python

How to group by two columns, calculate weighted mean, return DataFrame, in Python

From Dev

How to group, sum up and calculate a mean of every other element in a column?

From Dev

How to calculate mean value per group in Teradata SQL?

From Dev

Calculate the mean of an array in MATLAB

From Dev

Calculate mean for subgroup within group

From Java

How to calculate mean of a double array with Collectors.averagingDouble?

From Dev

How to calculate the mean by index of N elements in an array in R

From Dev

How to calculate mean of columns from array lists in python using numpy?

From Dev

How to calculate mean/variance/standard deviation per index of array?

From Dev

How to calculate secondary diagonals mean in a 2D array?

From Dev

Dart How to calculate mean?

From Dev

How to calculate mean in a dataframe?

From Dev

How to calculate mean in python?

From Dev

how to calculate the mean with conditions?

From Dev

PureScript - Calculate the mean of an array of numbers

From Dev

calculate daily mean of an array in python

From Dev

Calculate overall mean of multiple columns by group

From Dev

for loop to calculate mean by group (also ignore NA)

From Dev

Group the data based on binary, and calculate mean, sd

From Dev

Group multiindex dataframes by labels to calculate mean

From Dev

Group list by a given element and calculate the mean

Related Related

  1. 1

    How to calculate the mean of a new group?

  2. 2

    How to calculate mean in group by another group?

  3. 3

    Group by and calculate mean / average of properties in a Javascript array

  4. 4

    in R, how to calculate mean of all column, by group?

  5. 5

    How to calculate mean spatial location by group

  6. 6

    How to group specific items in a column and calculate the mean

  7. 7

    Calculate Group Mean and Overall Mean

  8. 8

    Calculate mean by group with dplyr

  9. 9

    How to group by two columns, calculate weighted mean, return DataFrame, in Python

  10. 10

    How to group, sum up and calculate a mean of every other element in a column?

  11. 11

    How to calculate mean value per group in Teradata SQL?

  12. 12

    Calculate the mean of an array in MATLAB

  13. 13

    Calculate mean for subgroup within group

  14. 14

    How to calculate mean of a double array with Collectors.averagingDouble?

  15. 15

    How to calculate the mean by index of N elements in an array in R

  16. 16

    How to calculate mean of columns from array lists in python using numpy?

  17. 17

    How to calculate mean/variance/standard deviation per index of array?

  18. 18

    How to calculate secondary diagonals mean in a 2D array?

  19. 19

    Dart How to calculate mean?

  20. 20

    How to calculate mean in a dataframe?

  21. 21

    How to calculate mean in python?

  22. 22

    how to calculate the mean with conditions?

  23. 23

    PureScript - Calculate the mean of an array of numbers

  24. 24

    calculate daily mean of an array in python

  25. 25

    Calculate overall mean of multiple columns by group

  26. 26

    for loop to calculate mean by group (also ignore NA)

  27. 27

    Group the data based on binary, and calculate mean, sd

  28. 28

    Group multiindex dataframes by labels to calculate mean

  29. 29

    Group list by a given element and calculate the mean

HotTag

Archive