0

I use no add in libraries so what i'm looking for is a pure JavaScript solution.

This is not a duplicate of the thread offered. I want this thread opened up as the people that locked it didn't completely read what I was looking for. Offering some trivial function to initialize a dense array is NOT what I'm after. Read what I wrote.

I'm looking for a constructor, not some function that operates on an array of a known dimension. I'm looking for something that "just happens" when a multidimensional array is touched, and that something has to be an honest to goodness constructor that runs in the "background"to create the array elements on demand with no effort on the part of the programmer in the "foreground".

Correct me if I'm wrong, but when using a two dimensional array, one must first initialize the second dimension before attempting to use it.

It would be something like:

myArray[123] = [];
myArray[123][456] = 'Hi';

Is there some way to create a constructor to do that extra initialization automatically that wouldn't trash the existing functionality for a 1D array?

Alternatively, is there a way to create a new 2DArray object that will automatically allow one to use both dimensions with no prep work?

When the application has no foreknowledge of what the subscripts can be is when this issue becomes problematic.

Bill Gradwohl
  • 385
  • 4
  • 18
  • @zerkms: No he does not create items. He only sets the `.length` to 124 and creates *one* property. – Bergi Jul 22 '13 at 23:01
  • 1
    @zerkms: [§15.4.5.1](http://es5.github.io/#x15.4.5.1) if you want :-) Search the web for "*sparse array*". – Bergi Jul 22 '13 at 23:06
  • @Bergi: nothing found for "sparse array" :-S – zerkms Jul 22 '13 at 23:07
  • @Bergi: Anyway, my point was - having `[undefined x 123, 'Hi']` doesn't look like a good array usage PS: special thanks for 15.4.5.1 – zerkms Jul 22 '13 at 23:10
  • @zerkms - engines will create one item and set the length to 124, and i belive every browser does that without creating 123 undefined items in the array, so if it's in the spec for ECMA or not isn't really relevant to your comment being wrong -> http://jsfiddle.net/DCAaK/ – adeneo Jul 22 '13 at 23:15
  • I couldn't quite follow what you guys were quibbling about, but what I want is a CONSTRUCTOR not a regular function. I want it to handle sparse arrays intelligently. The brute force functions offered always create dense arrays. That's not the goal. The goal is a multidimensional array, sparse if that's what it would otherwise be, that automatically is usable with no prior setup except for declaring that it is [][]; – Bill Gradwohl Jul 23 '13 at 15:08
  • @BillGradwohl: I've read your question. There's no difference between a (factory) function and a constructor function in JavaScript. "*Is there some way to create a constructor to do that extra initialization automatically [when the property is accessed]*" - no, that's impossible or at least unusable. The duplicate offers a way to "*allow one to use both dimensions with no prep work*", to answer your alternative question. – Bergi Jul 23 '13 at 22:19
  • Only thing you could do is a helper function `assign(myArray, 123, 456, 'Hi')` which takes care of automatically initialising properties. – Bergi Jul 23 '13 at 22:21
  • @Bergi you are right something like the following helper function function assign(theArray, posX, posY, value) { var theArray = theArray || []; theArray[posX] = theArray[posX] || []; theArray[posX][posY] = theArray[posX][posY] || value; } var myArray = [];assign(myArray, 123, 456, 'Hi'); – Schwertfisch Jul 13 '19 at 21:54

1 Answers1

1

See that question: Is there a more concise way to initialize empty multidimensional arrays?

Solution:

function createArray(length) {
  var arr = new Array(length || 0),
      i = length;

  if (arguments.length > 1) {
    var args = Array.prototype.slice.call(arguments, 1);
    while(i--) arr[i] = createArray.apply(this, args);
  }        
  return arr;
 }

Simply call with an argument for the length of each dimension. Usage examples:

var multiArray = createArray(100,100); Gives a 2-dimensional array of size 100x100

Community
  • 1
  • 1
mor
  • 2,313
  • 18
  • 28
  • This is a simple function, not a constructor. It has to be purposefully executed to achieve its goal and one needs to know the dimensions before hand. A Constructor runs in the background, so to speak, to take care of the extra dimensionality with no effort on the part of the programmer in the foreground, and would do its job any time a new subscript is used thereby offering the added benefit of working when the limits of the dimensions are unknown. – Bill Gradwohl Jul 23 '13 at 15:00
  • "*would do its job any time a new subscript is used*" - I've never seen such a constructor; in no language. It's quite impossible in Javascript without [proxies](http://stackoverflow.com/q/13059837/1048572) – Bergi Jul 23 '13 at 16:50