1

What I mean by dynamic-dimensional arrays is multidimensional arrays that can have various dimensions. I need to create a function that does something to elements of multidimensional arrays, regardless of their dimensions. I wrote a function that should loop through all elements of a multidimensional array, but I can't find a way to get them. Here's what I wrote:

function loopThrough (multiArray, dimensions) {

  var i, indexes = new Array(dimensions.length);
  // stores the current position in multiArray as index combination

  for (i in indexes) indexes[i] = 0; // position is initialised with [0, 0, ... 0]

  while (i >= 0) {

    doStuff(multiArray[indexes[0], indexes[1], ... indexes[?]]); // this is where I got stuck

    for (i = indexes.length - 1; i >= 0 && ++indexes[i] >= dimensions[i]; indexes[i--] = 0);
    // creates the next index combination

  }

}

I also need a way to create such arrays. Say in an object's constructor, like:

function MultiArray (dimensions) {

  this.array = [];

  // create multidimensional array

}

For example, if I want to create a 5x3x8 array I should be able to call MultiArray([5,3,8]); just the same as calling MultiArray([4,6]); for a 4x6 array, or MultiArray([7]); for a plain 7-lengthed array.

3
  • An example of dimentions? Commented Oct 7, 2014 at 20:55
  • 1
    You have to use recursion Commented Oct 7, 2014 at 21:04
  • @hindmost you don't have to, but that is one way. Commented Oct 7, 2014 at 22:06

2 Answers 2

2

You can use something like this:

function MultiArray(dimensions) {
    var a = [];
    if (dimensions > 1) {
        a.push(MultiArray(dimensions -1));
    }
    return a;
}
var m = MultiArray(4);
Sign up to request clarification or add additional context in comments.

1 Comment

+1, though the name might better be lowercased as it is a factory function, not a constructor.
0
function MultiArray(dimensions) {
    this.elements = [];
    var leaf = dimensions.length == 1;
    var dimension = dimensions.shift();
    for (var i = 0; i < dimension; ++i) {
        this.elements.push(leaf ? undefined : new MultiArray(dimensions));
    }
}

MultiArray.prototype.get(indexes) {
    var leaf = indexes.length == 1;
    var index = indexes.shift();
    return leaf ? this.elements[index] : this.elements[index].get(indexes);
}

MultiArray.prototype.set(indexes, value) {
    var leaf = indexes.length == 1;
    var index = indexes.shift();
    if (leaf) {
        this.elements[index] = value;
    } else {
        this.elements[index].set(indexes, value);
    }
    return this;
}

var m = new MultiArray([4, 3, 5]);
m.set([1, 2, 4], "i'm a value in a multi dimensional array");
m.get([1, 2, 4]); // should return "i'm a value in a multi dimensional array"
m.get([2, 0, 3]); // should return undefined
m.get([0, 1]); // should return an array of 5 elements

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.