Looping through a matrix with JavaScript

by Janeth Kent Date: 03-05-2021 javascript coding functions


We were already talking about how to multiply arrays in JavaScript and we realised that we had not explained something as simple as traversing an array with Javascript. So we couldn't let the time pass and we have started working to explain you how to do this task.

The first thing to do is to define the array in Javascript. To do this we instantiate it directly on a constant.

const matrix = [[1,2,3],[4,5,6],[7,8,9]];

Here we see that the elements of the array are themselves arrays of elements, which ends up forming the matrix.

Being an array of arrays, we will need two for loops to traverse the matrix. The first one will traverse the main array and the second one will traverse each of the secondary arrays. The arrays will go from the initial position to the length of the array, which we get through the .length property.

In this way the two concatenated loops are as follows:

for (x=0;x<matrix.length;x++) {      
     for (y=0;y<matrix[x].length;y++) {        
       // Acceder al elemento      
     }  
}

Inside the second loop, we can access the element via the x and y variables of the matrix.

for (x=0;x<matrix.length;x++) {      
     for (y=0;y<matrix[x].length;y++) {          
          console.log(matrix[x][y]);      
     }  
}

This code returns the list of the elements we have traversed in the matrix. So, in order not to leave it without formatting, we are going to add a little bit of formatting in the generation of the array information. The final code will look like this:

for (x=0;x<matrix.length;x++) {      
     text = ""      
     for (y=0;y<matrix[x].length;y++) {          
          text += matrix[x][y] + "t";      
      }      
      console.log(text)  
}

I hope you found useful this simple code that explains how to traverse a matrix with JavaScript.

 
by Janeth Kent Date: 03-05-2021 javascript coding functions hits : 2715  
 
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN END DEVELOPMENT.

 
 
 

Related Posts

What are React Hooks and what problems they solve

Working with React, - and before the release of Hooks in version 16.8 -  there was always the possibility to create components in three different ways based on a number of…

Flattening arrays in JavaScript

When we are handling arrays that are arrays or have multiple dimensions it can be very useful to know how to flatten arrays in JavaScript. That is to say, to…

How to populate an array with random numbers in JavaScript

Some of you might think that what we explained in the article on how to populate an array with numbers, apart from the didactic part, would not have much applicability…

How to populate an array with numbers in JavaScript

Populate an array with numbers in JavaScript The first step is to initialise the array. So today we are going to see a simple way to do it and see how…

Top Javascript Libraries and Frameworks Part 2

What are JavaScript frameworks?   JavaScript frameworks are application frameworks that allow developers to manipulate code to meet their particular needs. Web application development is like building a house. You have the option…

How to Send Email from an HTML Contact Form

In today’s article we will write about how to make a working form that upon hitting that submit button will be functional and send the email (to you as a…

Top JavaScript libraries and frameworks part 1

JavaScript libraries and frameworks make it easy to develop websites and applications with a wide variety of features and functionality, all with dynamic, flexible and engaging features. According to a 2020…

How to multiply matrices in JavaScript

It may seem strange to want to know how to multiply matrices in JavaScript. But we will see some examples where it is useful to know how to perform this…

JavaScript Formatting Date Tips

Something that seems simple as the treatment of dates can become complex if we don't take into account how to treat them when presenting them to the user. That is…

How to make a multilingual website without redirect

Today, we're going to talk about how to implement a simple language selector on the basic static website, without the need of any backend or database calls or redirection to…

Starting with Bootstrap-Vue step by step

Today we will show you how to use BootstrapVue, describe the installation process and show basic functionality. The project’s based on the world's most popular CSS framework - Bootstrap, for building…

Creating simple CSS spinner-loader

In today's article we will show you how to animate a basic loader that spins when some predefined action is defined, such as loading an image. That can be used…

We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration