如何使用 JavaScript 将数组元素解包到单独的变量中?
数组元素解包意味着将数组元素值分配给新的变量。我们可以将每个元素分配给单独的变量,或者可以将某些元素分配给单独的变量,对于其余元素,我们可以创建一个新的数组。在本教程中,我们将学习如何使用 JavaScript 将数组元素解包到单独的变量中。
语法
用户可以按照以下语法将数组元素解包到单独的变量中。
let array = [element1, element2, element3, element4]; let [a, b, c, d] = array;
在上述语法中,变量 a 包含 element1 的值,b 包含 element2 的值,c 包含 element3 的值,而 d 变量包含 element4 的值。
现在,我们将看看使用 JavaScript 将数组元素解包到单独的变量中的各种示例。
示例 1
在下面的示例中,我们定义了数组 array1 并用一些数值对其进行了初始化。此外,我们还定义了变量 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>
我们学习了数组解构,它是在 JavaScript 的 ES6 版本中引入的。我们已经看到了四个代表数组元素解包用例的示例。
用户学习了如何在解包数组元素时跳过元素并为变量分配默认值。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP