Skip to content Skip to sidebar Skip to footer

Average Of Bidimensional Array's Columns With Array.map()

I have an array that looks like this: var array = [ [1,3,9], [4,6,8], [3,7,5], [2,8,4] ]; I want to get the average number of each column

Solution 1:

You can use reduce() and forEach() to return result.

var array = [
  [1, 3, 9],
  [4, 6, 8],
  [3, 7, 5],
  [2, 8, 4]
];

var sums = array.reduce(function(r, e, i) {
  e.forEach((a, j) => r[j] = (r[j] || 0) + a)
  return r;
}, [])

console.log(sums)

To calculate avg for each column you can add map() on last iteration of array.

var array = [
  [1, 3, 9],
  [4, 6, 8],
  [3, 7, 5],
  [2, 8, 4]
];

var sums = array.reduce(function(r, e, i) {
  e.forEach((a, j) => r[j] = (r[j] || 0) + a)
  if (i == array.length - 1) r = r.map(el => el / array.length);
  return r;
}, [])

console.log(sums)

Solution 2:

To get the average of each column you'd first have to know the amount of columns. Then grab each column with map() and to sum everything with reduce()

Now we have the column, the sum and then just divide by column length.

constarrayColumn = (a, n) => a.map(x => x[n]);
constarraySum    = (a) => a.reduce((b,c) => b + c);

var arr = [ 
 [1,3,9],
 [4,6,8],
 [3,7,5],
 [2,8,4] 
];

for(i=0; i<arr[0].length; i++){
  console.log(arraySum((col = arrayColumn(arr, i))) / col.length);
}

Solution 3:

This will convert the 2d array of rows into a 2d array of columns and then maps each inner array of columns to an average. There is a little bit of boilerplate to make the inner reduce immutable you could use lodash/fp or another library to clean this up.

const array = [ 
  [1,3,9],
  [4,6,8],
  [3,7,5],
  [2,8,4] 
];

constaverageColumns = array => array.reduce((acc, row) => {
  return row.reduce((accRow, col, index) => {
    const cols = accRow[index] || [];
    return [...accRow.slice(0, index), cols.concat(col), ...accRow.slice(index + 1)];

  }, acc);
}, []).map(avg);

constavg = array => array.reduce((acc, next) => acc + next, 0) / array.length;

console.log(averageColumns(array));

Solution 4:

map() + reduce() solution

var array = [ [1,3,9], [4,6,8], [3,7,5], [2,8,4] ];


array.map(function(item, i, arr) {
	arr[i] = item.reduce((a, b) => a + b, 0) / 2;
  	console.log(arr[i])
});

I'm a little fix up your code you have mistake here return sum(array.map(function(v) { return v[i]; }))

vararray = [ [1,3,9],
              [4,6,8],
              [3,7,5],
              [2,8,4] ];
functionsum(arr) {
  return arr.reduce(function(a, b) { return a + b; }, 0);
};

var tests = array.map(function(v, i, arr) {
  return sum(arr[i])
});

tests;

Solution 5:

You can transpose the array of arrays, popular utility libraries (ramda for ex) have a transpose implementation, but it's easy to implement your own:

consttrans = arr => arr[0].map((_,i) => arr.map(x => x[i]))

var array = [ 
 [1,3,9],
 [4,6,8],
 [3,7,5],
 [2,8,4] 
];
const res = trans(array)
console.log(res)


// to get the sum you can use reduceconsole.log(res.map( x => x.reduce((a,b) => a + b )))

Post a Comment for "Average Of Bidimensional Array's Columns With Array.map()"