如何使用JavaScript将数组元素解包到单独的变量中

如何使用JavaScript将数组元素解包到单独的变量中

解包数组元素意味着将数组元素值分配给新变量。我们可以将每个元素分配给单独的变量,或者我们可以将一些元素分配给单独的变量,对于剩余的元素,我们可以创建一个新数组。在本教程中,我们将学习如何使用JavaScript将数组元素解包到单独的变量中。

语法

用户可以按照以下语法将数组元素解包到单独的变量中。

let array = [element1, element2, element3, element4];
let [a, b, c, d] = array;

在上面的语法中,一个变量包含元素1的值,b包含元素2的值,c包含元素3的值,d变量包含元素4的值。

现在,我们将看一下使用JavaScript将数组元素解包到单独变量中的各种示例。

示例1

在下面的示例中,我们定义了数组1并用一些数值初始化它。同时,我们定义了a、b和c变量,用于解包数组元素。之后,我们使用上述语法将数组元素解构为单独的变量。

在输出中,用户可以观察到a、b和c变量的初始值,以及我们将数组元素解包到它们后的最终值。

<html>
<body>
   <h2>Unpacking the array elements <i> to separate variables </i> using JavaScript. </h2>
   <div id = "output"> </div>
   <script>
      var output = document.getElementById('output');
      let array1 = [10, 20, 30];
      let a = 40;
      let b = 100;
      let c = 50;
      output.innerHTML += "The elements of the array1 are " + array1 + " <br/>";
      output.innerHTML += "The initial values of the a, b, and c variables are a : " + a + " b : " + b + " c : " + c + " <br>";
      [a, b, c] = array1;
      output.innerHTML += "The values of the a, b, and c variables after unpacking the array elements are a : " + a + " b : " + b + " c : " + c + " <br>";
   </script>
</body>
</html>

示例2

在这个示例中,我们在声明新变量的同时,直接解构数组元素为单独的变量。用户可以观察到我们是如何声明多个变量并解构数组的,通过直接将数组赋值给变量而不是赋值给数组变量。

<html>
<body> 
   <h2>Unpacking the array elements <i> to separate variables </i> using JavaScript. </h2>
   <div id = "output"> </div>
   <script>
      var output = document.getElementById('output');
      let [var1, var2, var3] = ["Hi", "Hello", "Welcome"];
      output.innerHTML += "The values of the var1, var2, and var3 variables after unpacking the array elements are var1 : " + var1 + ", var2 : " + var2 + ", var3 : " + var3 + " <br>";
   </script>
</body>
</html>

示例3

在下面的示例中,当用户点击按钮时,它将调用unpackElements()函数。在unpackElements()函数中,我们解构了数组。此外,我们在解构数组元素到单独的变量时,跳过了一些元素。

用户可以看到,我们可以通过逗号分隔来跳过任何数组索引的元素以添加空间。

<html>
<body>
   <h2>Skipping some array elements while unpacking them <i> into separate variables </i> using JavaScript. </h2>
   <div id = "output"> </div>
   <button onclick="unpackElements()">Unpack array elements</button>
   <script>
      var output = document.getElementById('output');
      function unpackElements() {
         let a, b, c, d;
         output.innerHTML += "The initial values of a, b, c, d variables are a : " + a + ", b : " + b + ", c : " + c + ", d : " + d + "<br/>";

         // Skipping the elements
         [a, , b, , c, , d] = [10, 20, 30, 40, 50, 60, 70, 80, 90];
         output.innerHTML += "The values of the a, b, c, and d variables after unpacking the array elements are a : " + a + ", b : " + b + ", c : " + c + ", d : " + d + "<br/>";
      }
   </script>
</body>
</html>

示例4

在下面的示例中,我们将学习在解包数组元素时给变量分配默认值。我们将500赋值为所有变量的默认值。如果数组包含的元素少于变量总数,则变量将使用默认值初始化。

在下面示例的输出中,我们可以看到d的值为500,这是默认值。

<html>
<body>
   <h3>Assigning the default values to the variables while unpacking array elements <i> to separate variables </i> using JavaScript </h3>
   <div id = "output"> </div>
   <button onclick="unpackElements()">Unpack array elements</button>
   <script>
      var output = document.getElementById('output');
      function unpackElements() {
         let a, b, c, d;
         output.innerHTML += "The initial values of a, b, c, d variables are a : " + a + ", b : " + b + ", c : " + c + ", d : " + d + "<br/>";

         // Skipping the elements
         [a = 500, , b = 500, , c = 500, , d = 500] = [10, 20, 30, 40, 50];
         output.innerHTML += "The values of the a, b, c, and d variables after unpacking the array elements are a : " + a + ", b : " + b + ", c : " + c + ", d : " + d + "<br/>";
      }
   </script>
</body>
</html>

我们已经学习了ES6版本引入的数组解构。我们已经看到了四个示例,表示了数组元素解包的用例。

用户学习了跳过元素并在解包数组元素时为变量赋予默认值。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程