7

What is the most efficient and/or most readable way to write a function that takes in an array and returns the degree of multi-dimensionality of that array. For now it can be assumed that the arrays only contain primitive types.

Example.

    var arr = [[1,2],[3,4],[5,6]]

    function findDim(a){
    //logic goes here
    }

    findDim(arr); // returns 2
2
  • 1
    What would be "dimensionality" of this one: [ 1, [2,3], [[4]] ]? Commented Aug 30, 2016 at 17:17
  • we can say this function should calculate the max dimenstionality - so 3. Commented Aug 30, 2016 at 20:18

3 Answers 3

9

Use recursion and Array.isArray method to check element is an array.

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

function findD(arr) {
  // check the element is an array then do 
  // recursion to check it's element
  if (Array.isArray(arr)) {
    return 1 + findD(arr[0]);
  }
  // else return `0` since it's not
  // a nested array
  return 0;
}

console.log(findD(arr));


FYI : For older browser check polyfill option of Array.isArray method.


UPDATE : Incase it contains different dimensioned array and you want to get the deeper dimension then use Array#map and Math.max methods.

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

function findD(arr) {
  // return 0 if not array else return the max value 
  // by finding all elements dimension
  return Array.isArray(arr) ?
    // generate the dimension value array 
    1 + Math.max.apply(Math, arr.map(findD)) : 0;
}

console.log(findD(arr));


Or with Array#reduce method to get the max value.

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

function findD(arr) {
  // return 0 if not array else return the max value 
  // by finding all elements dimension
  return Array.isArray(arr) ? 1 + arr.reduce(function(a, b) {
    // get the largest by comparing all the adjuscent 
    // elements dimension
    return Math.max(a, findD(b));
  }, 0) : 0;
}

console.log(findD(arr));

Sign up to request clarification or add additional context in comments.

2 Comments

Now it' is much better
function(v) { return findD(v) } doesn't make much sense though.
2

"Dimensionality" is not well defined for js arrays (which are not necessary matrices), here's a function to find the max "depth" of the array:

maxDepth = x => Array.isArray(x)
  ? 1 + Math.max.apply(this, x.map(maxDepth))
  : 0
;

console.log(maxDepth([[1,2],[3,4],[5,6]]))
console.log(maxDepth([[[[1]]], 2]))

2 Comments

This is the only one answer that works (well.. along with mine -:) )
@Redu: well, the OP didn't say how they (or their teacher) want to handle irregular arrays... not exactly sure what is more "correct" here.
1

This is how i would do. It's valid for irregularly multidimensional arrays;

var arr = [[1,2],[3,4],[5,[6,[7,[8]]]]],
findDim = a => Math.max(...a.map(e => Array.isArray(e) ? findDim(e) : 0)) + 1
console.log(findDim(arr))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.