Monday 20 November 2017

javascript - Constructor for two dimensional array

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.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...