Waiting Answer November 04, 2023

How to Define Multidimensional Array in JavaScript?

Answers
2024-02-05 12:21:21

In JavaScript, creating a multidimensional array involves nesting arrays within other arrays. Although JavaScript doesn’t directly provide built-in multidimensional arrays, we can simulate them by using arrays inside another array. Let’s explore a couple of ways to define and work with multidimensional arrays:

  1. Array Literals:

    • The easiest way to define a multidimensional array is to use the array literal notation.
    • To declare an empty multidimensional array, you can use the same syntax as declaring a one-dimensional array:JavaScript

       

      let activities = []; // Empty multidimensional array
      
      
    • For example, you can create a two-tier array like this:JavaScript

       

      const matrix = [
        [1, 2, 3],
        [4, 5, 6]
      ];
      In the matrix array, we have two inner arrays, each containing three elements
      
  2. Defining Arrays Inside Another Array:

    • You can manually create a multidimensional array by defining arrays inside another array.

    • Let’s say we want to create a salary data structure with multiple employees. We can use either of the following methods:

       

      Method 1 (Using Separate 1D Arrays):

      JavaScript

       

      let arr1 = ["ABC", 24, 18000];
      let arr2 = ["EFG", 30, 30000];
      let arr3 = ["IJK", 28, 41000];
      let arr4 = ["EFG", 31, 28000];
      let arr5 = ["EFG", 29, 35000];
      
      // Combine the individual arrays into a multidimensional array
      let salary = [arr1, arr2, arr3, arr4, arr5];
      

       

      Method 2 (Using Array Literals):

      JavaScript

       

      let salary = [
        ["ABC", 24, 18000],
        ["EFG", 30, 30000],
        ["IJK", 28, 41000],
        ["EFG", 31, 28000]
      ];
      

       

  3. Accessing Elements in a Multidimensional Array:

    • To access elements, use index-based notation. For example:
      • salary[0][2] retrieves the salary of the person with the name “ABC” (which is 18000).
      • salary[3][2] retrieves the salary of the person with the name “EFG” (which is 28000).
    • For iterating through the entire array, use nested loops:JavaScript

       

      for (let i = 0; i < salary.length; i++) {
        for (let j = 0; j < salary[i].length; j++) {
          console.log(salary[i][j]);
        }
      }
      

       

  4. Adding Elements to a Multidimensional Array:

    • You can add elements to the inner arrays or the outer array.
    • To add an element to the 4th sub-array (e.g., “EFG”), you can use either of the following:JavaScript

       

      // Using square bracket notation
      salary[3][3] = "India"; // Adds "India" at the 4th index of the 4th sub-array
      
      // Using push() method
      salary[3].push("India", "Mumbai"); // Adds "India" and "Mumbai" to the 4th sub-array
      
      

Remember that multidimensional arrays in JavaScript are essentially arrays inside another array, allowing you to organize data in a structured manner.