{"id":243,"date":"2015-12-31T16:03:39","date_gmt":"2015-12-31T21:03:39","guid":{"rendered":"http:\/\/rpchurchill.com\/?p=243"},"modified":"2017-02-17T02:12:31","modified_gmt":"2017-02-17T07:12:31","slug":"multidimensional-arrays-in-javascript","status":"publish","type":"post","link":"https:\/\/rpchurchill.com\/wordpress\/posts\/2015\/12\/31\/multidimensional-arrays-in-javascript\/","title":{"rendered":"Multidimensional Arrays in Javascript"},"content":{"rendered":"<p>Defining arrays in Javascript turns out to be a bit tricky. You can&#8217;t just predeclare the dimensions, you have to initiate them by defining the actual values for each element, dimension by dimension. For a one-dimensional array with three elements we can do this a couple of different ways.<\/p>\n<pre>var array1Da = [0,0,0];\r\n\r\nvar array1Db = new Array();\r\nfor (var i=0; i&lt;3; i++) {\r\n  array1Db[i] = 0;\r\n}\r\n<\/pre>\n<p>In both cases the mechanism that defines the actual size of the array is the act of assigning values to the elements. Initialization of two-dimensional arrays works the same way.<\/p>\n<pre>var array2Da = [[0,0,0],[0,0,0],[0,0,0]];\r\n\r\nvar array2Db = new Array();\r\nfor (var i=0; i&lt;3; i++) {\r\n  array2Db[i] = new Array(); {\r\n    for (var j=0; j&lt;3; j++) {\r\n      array2Db[i][j] = 0;\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>This works because all of the elements are initially populated in order.  The interpreter continually expands the definition as elements are defined.  These methods work for arrays of three or more dimensions as well.  That said, declaring and populating arrays of three or more dimensions is best accomplished using the new and loop method.  Trying to declare higher-dimensioned arrays directly gets painful rather quickly.  This is even true of one- or two-dimensional arrays if one dimension is quite large.<\/p>\n<p>One might be tempted to try the following shortcut on the theory that the interpreter will just have to allocate what it needs, but we find that it doesn&#8217;t work.<\/p>\n<pre>\r\n  var randomArray[6][6][6] = 0;\r\n<\/pre>\n<p>The process feels a bit cumbersome but it is what it is.  Once an array is defined as it needs to be it can used anywhere in a program and can even be passed as a parameter to a function, where statements internal to that function will interpret the array and its dimensioning correctly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Defining arrays in Javascript turns out to be a bit tricky. You can&#8217;t just predeclare the dimensions, you have to initiate them by defining the actual values for each element, dimension by dimension. For a one-dimensional array with three elements &hellip; <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2015\/12\/31\/multidimensional-arrays-in-javascript\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[50,51,49],"_links":{"self":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/243"}],"collection":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/comments?post=243"}],"version-history":[{"count":1,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":244,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/243\/revisions\/244"}],"wp:attachment":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/tags?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}