Living in Two Dimensions (arrays that is)

accordion file

I had mentioned in previous posts about looping and I have also discussed arrays before. Today I wanted to discuss two-dimensional arrays. Two-dimensional arrays allow you to house multiple pieces of information at multiple levels.

I like to think of arrays as accordion file folders. One item with multiple segments. A 2D array is like putting an accordion file folder inside another. Let’s see how these work…

var myArray = new Array(2); // Creates an array with two sections
myArray[0] = new Array(); // Creates an empty array and inserts it into the first slot of the top-most array
myArray[0][0] = "I am the value of myArray[0][0]"; // Inserts a string into the inner array
myArray[1] = "I am the value of myArray[1]"; // Inserts a string in the second index of the top-most array

As you can see from the aforementioned code, 2D arrays allow us to put several layers into one variable. Another way of thinking about it is that a 2D array is an array of an array.

You saw how I created it with the ‘new’ keyword, but there are other ways to create an array.

var myArray = new Array(new Array(3), new Array(6), new Array(9));

We could even initialize it on the fly…

// Single array
var myArray = ['firstvalue','secondvalue'];
// 2D array
var myArray = [['firstvalue1d','secondvalue1d'],['firstvalue2d','secondvalue2d']];

Perhaps another way of thinking about a 2D array is like a table with columns and rows.

Now, anytime you have an array, chances are that you will need to loop over it. So how do we approach looping over a 2D array? Since we have two (2) dimensions, we will need two (2) loops. We have to nest them. Here is an example…

for(var i=0; i < outerArray.length; i++){ // loops over outer array
    for(var j=0; j < outerArray[i].length; j++){ // loops over inner array
        // output array data

On a side note, we can add data to the end of an array by using the ‘push’ method…myArray.push(‘someValue’);

Happy Coding!

Clay Hess

More To Explore

computer, laptop, work place-2982270.jpg

Unlocking Wireless Communication: A Dive into the Bluetooth API

Wireless communication has become an integral part of our daily lives, and Bluetooth technology is at the forefront of this revolution, enabling devices to exchange data over short distances and creating a world more interconnected than ever before. At the heart of this technology lies the Bluetooth Application Programming Interface (API), a powerful tool for developers looking to harness the capabilities of Bluetooth in their applications. In this blog post, we’ll explore what the Bluetooth API is, how it works, and the possibilities it opens up for innovation in wireless communication.

lighthouse, beacon, atlantic-8578318.jpg

Understanding the Beacon API: Simplifying Asynchronous Data Transfers

In today’s data-driven world, web applications often need to send data back to the server. Traditionally, this has been done using AJAX requests or similar methods. However, these techniques can come with a cost, especially when dealing with data that needs to be sent during the unload phase of a document, such as tracking and diagnostic data. This is where the Beacon API shines by allowing developers to send data to a server more reliably and efficiently.

Share This Post


Need help?

Let's have a chat...


Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 


Begin Your Learning Journey Today!

Come back inside to continue your learning journey.