Skip to content
the poor coder

javascript como recorrer un array multidimensional

Poor Man
Published
Reading time
2 min read
On this page

Javascript: Cómo recorrer un array multidimensional#

Si estás trabajando con un array multidimensional en Javascript, es posible que necesites recorrerlo para realizar ciertas operaciones. Para hacer esto, hay algunas formas diferentes de hacerlo. Aquí te muestro algunas.

Método 1: Bucle anidado#

La forma más simple de recorrer un array multidimensional es usando un bucle anidado for. En este ejemplo, supongamos que tienes un array de dos dimensiones llamado "miArray":

Text
var miArray = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

Para recorrerlo, simplemente tendrías que hacer dos bucles for anidados:

Text
for (var i = 0; i < miArray.length; i++) {
  for (var j = 0; j < miArray[i].length; j++) {
    console.log(miArray[i][j]);
  }
}

Este código imprimirá todos los elementos del array en la consola.

Método 2: Función recursiva#

Otra forma de recorrer un array multidimensional es usando una función recursiva. Esta solución es más elegante y reutilizable que el bucle anidado, pero puede ser un poco más difícil de entender al principio.

Text
function recorrerArray(array) {
  for (var i = 0; i < array.length; i++) {
    if (Array.isArray(array[i])) {
      recorrerArray(array[i]);
    } else {
      console.log(array[i]);
    }
  }
}

recorrerArray(miArray);

En este ejemplo, la función "recorrerArray" recorre el array que se le pasa como parámetro. Si un elemento del array es otro array, la función se llama a sí misma para recorrer ese array. Si el elemento es un valor simple, se imprime en la consola.

Este código también imprimirá todos los elementos del array en la consola.

Método 3: forEach y reduce#

Por último, puedes recorrer un array multidimensional usando los métodos forEach y reduce de Javascript. Este enfoque es más moderno que los anteriores, pero también es un poco más complicado de entender.

Text
miArray.forEach(function (fila) {
  fila.reduce(function (anterior, actual) {
    console.log(actual);
  });
});

En este ejemplo, usamos el método forEach para recorrer las filas del array. Luego usamos el método reduce para recorrer los elementos de cada fila y imprimirlos en la consola.

Este código también imprimirá todos los elementos del array en la consola.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC