Matrice multidimensionale JavaScript

In questo tutorial imparerai a conoscere gli array multidimensionali JavaScript con l'aiuto di esempi.

Un array multidimensionale è un array che contiene un altro array. Per esempio,

 // multidimensional array const data = ((1, 2, 3), (1, 3, 4), (4, 5, 6));

Crea un array multidimensionale

Ecco come creare array multidimensionali in JavaScript.

Esempio 1

 let studentsData = (('Jack', 24), ('Sara', 23), ('Peter', 24));

Esempio 2

 let student1 = ('Jack', 24); let student2 = ('Sara', 23); let student3 = ('Peter', 24); // multidimensional array let studentsData = (student1, student2, student3);

Qui, sia l'esempio 1 che l'esempio 2 creano un array multidimensionale con gli stessi dati.

Accedi agli elementi di un array

È possibile accedere agli elementi di un array multidimensionale utilizzando gli indici (0, 1, 2 …) . Per esempio,

 let x = ( ('Jack', 24), ('Sara', 23), ('Peter', 24) ); // access the first item console.log(x(0)); // ("Jack", 24) // access the first item of the first inner array console.log(x(0)(0)); // Jack // access the second item of the third inner array console.log(x(2)(1)); // 24

Puoi pensare a un array multidimensionale (in questo caso, x), come una tabella con 3 righe e 2 colonne.

Accesso a elementi di array multidimensionali

Aggiungi un elemento a un array multidimensionale

È possibile utilizzare il metodo push () di Array o una notazione di indicizzazione per aggiungere elementi a un array multidimensionale.

Aggiunta di elementi all'array esterno

 let studentsData = (('Jack', 24), ('Sara', 23),); studentsData.push(('Peter', 24)); console.log(studentsData); //(("Jack", 24), ("Sara", 23), ("Peter", 24)

Aggiunta di un elemento alla matrice interna

 // using index notation let studentsData = (('Jack', 24), ('Sara', 23),); studentsData(1)(2) = 'hello'; console.log(studentsData); // (("Jack", 24), ("Sara", 23, "hello"))
 // using push() let studentsData = (('Jack', 24), ('Sara', 23),); studentsData(1).push('hello'); console.log(studentsData); // (("Jack", 24), ("Sara", 23, "hello"))

È inoltre possibile utilizzare il metodo splice () di Array per aggiungere un elemento a un indice specificato. Per esempio,

 let studentsData = (('Jack', 24), ('Sara', 23),); // adding element at 1 index studentsData.splice(1, 0, ('Peter', 24)); console.log(studentsData); // (("Jack", 24), ("Peter", 24), ("Sara", 23))

Rimuovi un elemento da un array multidimensionale

È possibile utilizzare il metodo pop () di Array per rimuovere l'elemento da un array multidimensionale. Per esempio,

Rimuovi elemento dall'array esterno

 // remove the array element from outer array let studentsData = (('Jack', 24), ('Sara', 23),); studentsData.pop(); console.log(studentsData); // (("Jack", 24))

Rimuovi elemento dalla matrice interna

 // remove the element from the inner array let studentsData = (('Jack', 24), ('Sara', 23)); studentsData(1).pop(); console.log(studentsData); // (("Jack", 24), ("Sara"))

È inoltre possibile utilizzare il splice()metodo per rimuovere un elemento in corrispondenza di un indice specificato. Per esempio,

 let studentsData = (('Jack', 24), ('Sara', 23),); // removing 1 index array item studentsData.splice(1,1); console.log(studentsData); // (("Jack", 24))

Iterazione su array multidimensionali

È possibile iterare su un array multidimensionale utilizzando il metodo forEach () di Array per iterare sull'array multidimensionale. Per esempio,

 let studentsData = (('Jack', 24), ('Sara', 23),); // iterating over the studentsData studentsData.forEach((student) => ( student.forEach((data) => ( console.log(data); )); ));

Produzione

 Jack 24 Sara 23

Il primo forEach()metodo viene utilizzato per iterare sugli elementi della matrice esterna e il secondo forEach()viene utilizzato per iterare sugli elementi della matrice interna.

È inoltre possibile utilizzare il for… ofciclo per eseguire l'iterazione sull'array multidimensionale. Per esempio,

 let studentsData = (('Jack', 24), ('Sara', 23),); for (let i of studentsData) ( for (let j of i) ( console.log(j); ) )

È inoltre possibile utilizzare il ciclo for per iterare su un array multidimensionale. Per esempio,

 let studentsData = (('Jack', 24), ('Sara', 23),); // looping outer array elements for(let i = 0; i < studentsData.length; i++)( // get the length of the inner array elements let innerArrayLength = studentsData(i).length; // looping inner array elements for(let j = 0; j < innerArrayLength; j++) ( console.log(studentsData(i)(j)); ) )

Articoli interessanti...