Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6759551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:57:22+00:00 2026-05-26T13:57:22+00:00

How would I go about calculating the average of all the values in a

  • 0

How would I go about calculating the average of all the values in a multidimensional array? I’ve written a function to calculate the average from a 1-dimensional array, but I’m not sure what the best method is when there are more than 1-dimensions.

For example, let’s say we have the following:

var A = Array(3);
for (i=0; i<A.length; i++) {
    A[i] = new Array(2);
    for (j=0; j<A[i].length; j++) {
        A[i][j] = i+j;
    }
}

Therefore, A is a 2-dimensional array, or 3×2 matrix:

A = 0   1
    1   2
    2   3

So I’d like to find the average of all the values, which in this case would equal 1.5. I imagine I need to create a new 1-dimensional array of all the values, which I could then feed into my averaging function. However, I’m not sure of the easiest way to do this when the array is highly-dimensional (e.g. 5x3x6x9).

Thanks!

EDIT

Thanks everyone! I’ve used your advice and flattened the array using code I found in one of the attached links which uses the reduce function. My averaging function is now like this:

function average(x) {

    // Flatten multi-dimensional array
    while (x[0] instanceof Array) {
        x = x.reduce( function(a, b) { return a.concat(b); } );
    }

    // Calculate average
    return x.reduce( function(a, b) { return a + b; } )/x.length;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:57:23+00:00Added an answer on May 26, 2026 at 1:57 pm

    You can use this code to flatten the multi-dimensional array:

    function flatten(array){
        var flat = [];
        for (var i = 0, l = array.length; i < l; i++){
            var type = Object.prototype.toString.call(array[i]).split(' ').pop().split(']').shift().toLowerCase();
            if (type) { flat = flat.concat(/^(array|collection|arguments|object)$/.test(type) ? flatten(array[i]) : array[i]); }
        }
        return flat;
    }
    

    and then just sum and divide:

    var total = 0;
    for (var i = 0, l = flattenedArray.length; i<l; i++) {
      total += flattenedArray[i];
    }
    var average = total/flattenedArray.length;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering how one would go about developing a website from scratch with
I am wondering how I would go about performing a certain function if a
How would you go about calculating the amount of months between two arbitrary dates?
Having a dataset and calculating statistics from it is easy. How about the other
How would I go about calculating a compound interest rate, so far I have:
How would I go about calculating the size of a web page (url) using
How would I go about calculating the size and starting byte of the data
I implemented a program about calculating the stock values comparing that value in the
How would you go about calculating / finding the number of operations a regex
Given two integers a and b , how would I go about calculating the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.