📜  JavaScript 中的多维数组

📅  最后修改于: 2022-05-13 01:57:05.322000             🧑  作者: Mango

JavaScript 中的多维数组

JavaScript 中不直接提供多维数组。如果我们想使用任何充当多维数组的东西,那么我们需要通过使用另一个一维数组来创建一个多维数组。所以 JavaScript 中的多维数组被称为另一个数组中的数组。我们需要将一些数组放入一个数组中,然后整个事情就像一个多维数组一样工作。其他数组将要插入的数组,该数组在我们的代码中用作多维数组。定义一个多维数组与定义一个普通的一维数组完全相同。

一维数组:

var arr = []; // Empty 1D array
var arr1 = ["A", "B", "C", "D"] // 1D array contains some alphabets
var arr1 = [1, 2, 3, 4, 5] // 1D array contains some digits

多维数组:

  • 方法一:
    1st, need to define some 1D array
    var arr1 = ["ABC", 24, 18000];
    var arr2 = ["EFG", 30, 30000];
    var arr3 = ["IJK", 28, 41000];
    var arr4 = ["EFG", 31, 28000];
    var arr5 = ["EFG", 29, 35000];
    // "salary" defines like a 1D array but it already contains some 1D array
    var salary = [arr1, arr2, arr3, arr4, arr5]; 

    这里 arr1, arr2, ...arr5 是工资数组中的一些一维数组。

  • 方法二:
    var salary = [
       ["ABC", 24, 18000],
       ["EFG", 30, 30000],
       ["IJK", 28, 41000],
       ["EFG", 31, 28000],
    ];

    在这里, salary数组就像一个多维数组。这种表示法称为数组字面量。

访问薪水数组的元素:

  • 要访问数组元素,我们需要一个简单的基于索引的符号
    // This notation access the salary of "ABC" person which is 18000, 
    // [0] selects 1st row, and [2] selects the 3rd element
    // of that 1st row which is 18000
    salary[0][2];
    
    // Similarly, 
    salary[3][2]; // Selects 28000
    
    **This notation is used for both Method 1 and Method 2.
    
  • 对于多次迭代,我们需要使用循环来访问元素,
    // This loop is for outer array
    for (var i = 0, l1 = salary.length; i < l1; i++) {
    
        // This loop is for inner-arrays
        for (var j = 0, l2 = salary[i].length; j < l2; j++) {
    
            // Accessing each elements of inner-array
            documents.write( salary[i][j] ); 
        }
    }

在多维数组中添加元素:在多维数组中添加元素可以通过内部数组外部数组两种方式实现。内部数组可以通过两种不同的方式完成。

  • 向内部数组添加元素:
    • 我们可以使用简单的方括号表示法在多维数组中添加元素。
      salary[3][3] = "India";
      
      // It adds "India" at the 4th index of 4th sub-array,
      // If we print the entire 4th sub-array, document.write(salary[3]);
      // the output will be :  ["EFG", 31, 28000, "India"]
      // indexing starts from 0
    • 我们可以使用push()方法在数组中添加元素。
      salary[3].push("India", "Mumbai");
      
      // It add "India" at the 4th index and "Mumbai" at
      // 5th index of 4th sub-array
      // If we print the entire 4th sub-array,
      // document.write(salary[3]);
      // The output will be :  ["EFG", 31, 28000, "India", "Mumbai"]
      // Indexing starts from 0
  • 向外部数组添加元素:它与以前的方法非常相似。
    salary.push(["MNO", 29, 33300]);
    // This row added after the last row in the "salary" array

删除多维数组中的元素:我们可以使用pop()方法从内部数组中删除元素,也可以使用pop()方法删除整个内部数组。

// Remove last element from 4th sub-array
// That is 28000 indexing starts from 0
salary[3].pop(); 

// Removes last sub-array
// That is "["EFG", 31, 28000]"
salary.pop();
  • 示例 1:
    // Prints a simple multidimensional array in JavaScript
    
    

    输出:

    ABC, 24, 18000
    EFG, 30, 30000
    IJK, 28, 41000
    EFG, 31, 28000
    EFG, 29, 35000
  • 示例 2:
    // Prints a simple multidimensional array in
    // JavaScript with different declaration
                        
    

    输出:

    ABC, 24, 18000
    EFG, 30, 30000
    IJK, 28, 41000
    EFG, 31, 28000
    EFG, 29, 35000
  • 示例 3:
    // Prints a simple multidimensional array in JavaScript
    // where we just print the salary of a specific person
                        
    

    输出:

    salary of 2nd person : 30000
    salary of 4th person : 28000
  • 示例 4:
    // Prints a simple multidimensional array in
    // JavaScript where we add elements in the array 
    // using simple square bracket and push() method
     
    

    输出:

    Original array :
    ABC, 24, 18000
    EFG, 30, 30000
    IJK, 28, 41000
    EFG, 31, 28000
    
    after adding "India" at the 4th array :
    ABC, 24, 18000
    EFG, 30, 30000
    IJK, 28, 41000
    EFG, 31, 28000, India
    
    after adding "USA" and "Canada" at the 3rd array using "push()" method :
    ABC, 24, 18000
    EFG, 30, 30000
    IJK, 28, 41000, USA, Canada
    EFG, 31, 28000, India
  • 示例 5:
    // Prints a simple multidimensional array in
    // JavaScript where we add a new inner array 
     
    

    输出:

    Original array :
    ABC, 24, 18000
    EFG, 30, 30000
    IJK, 28, 41000
    EFG, 31, 28000
    
    After adding a new inner array :
    ABC, 24, 18000
    EFG, 30, 30000
    IJK, 28, 41000
    EFG, 31, 28000
    MNO, 29, 33300
  • 示例 6:
    // Prints a simple multidimensional array in
    // JavaScript where we remove a single element
    //and a entire sub-array
     
    

    输出:

    Original array :
    ABC, 24, 18000
    EFG, 30, 30000
    IJK, 28, 41000
    EFG, 31, 28000
    
    After removing last element of last inner array :
    ABC, 24, 18000
    EFG, 30, 30000
    IJK, 28, 41000
    EFG, 31
    
    After removing last inner array :
    ABC, 24, 18000
    EFG, 30, 30000
    IJK, 28, 41000

JavaScript 以网页开发而闻名,但它也用于各种非浏览器环境。您可以按照这个 JavaScript 教程和 JavaScript 示例从头开始学习 JavaScript。