ts 数组解构时定义类型
Today I will talk about destructuring an array, first introduced in the ES6 version of Javascript. Destructuring is a way of extracting values from an array into variables.
今天,我将讨论解构数组,该数组是在Java ES6版本中首次引入的。 销毁是将数组中的值提取为变量的一种方法。
As mentioned destructuring came with ES6 version of Javascript, so earlier versions of Javascript do not support the destructuring syntax, in that cases either you have to use the traditional way to unpack object and arrays, or you use Babel or anything that will compile your code to the previous version for use.
如前所述,ES6版本的Javascript附带提供了解构功能,因此较早的Javascript版本不支持解构语法,在这种情况下,您要么必须使用传统的方式来解压缩对象和数组,要么使用Babel或任何会编译代码的东西到以前的版本使用。
解构数组 (Destructuring Array)
Array properties are accessed using index values, but what if you have an array and you want to assign the elements to different variables. If you use the old way of assigning the values one by one, it is tedious. Like in the below code
first
andsecond
have the values1
and2
from the array but that can be done in a single line by destructuring the arrayarr
.数组属性是使用索引值访问的,但是如果您有一个数组并且想要将元素分配给不同的变量,该怎么办。 如果您使用旧的逐个分配值的方法,那将很麻烦。 像下面的代码一样,
first
和second
具有数组中的值1
和2
,但是可以通过解构数组arr
在一行中完成。
const arr = [1, 2, 3, 4, 5, 6];// The way we did before ES6
const first = arr[0];