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 8980323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:03:42+00:00 2026-06-15T20:03:42+00:00

Often, there are situations where I need to determine whether a Javascript array is

  • 0

Often, there are situations where I need to determine whether a Javascript array is rectangular (and get the dimensions of the array). In this case, I mean determining whether every element of the array is an array with the same length. How can I do this?

function getArrayDimensions(theArray){
    //if the array's dimensions are 3x3, return [3, 3], and do the same for arrays of any dimension
    //if the array is not rectangular, return false
}

Additionally, how can this function be generalized to multidimensional arrays (2x5x7, 3x7x8x8, etc.)?

  • 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-06-15T20:03:43+00:00Added an answer on June 15, 2026 at 8:03 pm

    This recursive function returns all dimensions of a given array or false if one or more dimensions are not straight (i.e. different sizes between array items). It uses a helper function to determine whether two simple arrays are the same (read the function comments before using it).

    // pre: a !== b, each item is a scalar
    function array_equals(a, b)
    {
      return a.length === b.length && a.every(function(value, index) {
        return value === b[index];
      });
    };
    
    function getdim(arr)
    {
      if (/*!(arr instanceof Array) || */!arr.length) {
        return []; // current array has no dimension
      }
      var dim = arr.reduce(function(result, current) {
        // check each element of arr against the first element
        // to make sure it has the same dimensions
        return array_equals(result, getdim(current)) ? result : false;
      }, getdim(arr[0]));
    
      // dim is either false or an array
      return dim && [arr.length].concat(dim);
    }
    
    console.log(getdim(123)); // []
    console.log(getdim([1])); // [1]
    console.log(getdim([1, 2])); // [2]
    console.log(getdim([1, [2]])); // false
    console.log(getdim([[1, 2], [3]])); // false
    console.log(getdim([[1, 2],[1, 2]])); // [2, 2]
    console.log(getdim([[1, 2],[1, 2],[1, 2]])); // [3, 2]
    
    console.log(getdim([[[1, 2, 3],[1, 2, 4]],[[2, 1, 3],[4, 4, 6]]])); // [2, 2, 3]
    
    console.log(getdim([[[1, 2, 3], [1, 2, 4]], [[2, 1], [4, 4]]])); // false
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Often I need to temporary comment some code, but there are situations like the
Some classes, like exceptions or templates, only need the header file (.h), often there
I often get into situation when I need to sort a Map on values.
There is quite often situation when you need to execute INSERT, UPDATE or DELETE
Sometimes there is situations where I need to take a value from a database
I often come across situations when I need to use the same variable but
Is there a way in php to count how often a value exists in
Is there any way to do it? I often have issues that work locally
Very often, I just create a new folder and test things out in there.
There is a constant change (!) in our database, new columns are often added.

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.