📜  TypeScript元组

📅  最后修改于: 2021-01-11 12:27:57             🧑  作者: Mango

TypeScript元组

我们知道数组包含相同数据类型的多个值。但是有时,我们可能需要在单个变量中存储不同数据类型的值的集合。数组将不提供此功能,但是TypeScript具有称为Tuple的数据类型以实现此目的。元组是一个数组,用于存储属于不同数据类型的多个字段。它类似于C编程语言中的结构。

元组是一种数据类型,可以像其他任何变量一样使用。它代表值的异构集合,也可以在函数调用中作为参数传递。

在抽象数学中,术语“元组”用于表示多维坐标系。 JavaScript没有元组作为数据类型,但是TypeScript中提供了元组。元组中元素的顺序很重要。

句法

let tuple_name = [val1,val2,val3, ...val n];

let arrTuple = [101, "JavaTpoint", 105, "Abhishek"];
console.log(arrTuple);

输出:

[101, 'JavaTpoint', 105, 'Abhishek']

我们还可以通过在Typescript中最初将元组声明为空元组来分别声明和初始化元组。

let arrTuple = []; 
arrTuple[0] = 101
arrTuple[1] = 105

访问元组元素

我们可以使用与数组相同的索引来读取或访问元组的字段。在元组中,索引从零开始。

let empTuple = ["Rohit Sharma", 25, "JavaTpoint"];
console.log("Name of the Employee is : "+empTuple [0]);
console.log("Age of the Employee is : "+empTuple [1]);
console.log(empTuple [0]+" is working in "+empTuple [2]);

输出:

Name of the Employee is: Rohit Sharma
Age of the Employee is: 25
Rohit Sharma is working in JavaTpoint

元组上的操作

元组有两个操作:

  • 推()
  • Pop()

推()

推操作用于将元素添加到元组。

let empTuple = ["Rohit Sharma", 25, "JavaTpoint"];
console.log("Items: "+empTuple);
console.log("Length of Tuple Items before push: "+empTuple.length);   // returns the tuple size
empTuple.push(10001);   // append value to the tuple 
console.log("Length of Tuple Items after push: "+empTuple.length);
console.log("Items: "+empTuple);

输出:

Items: Rohit Sharma, 25, JavaTpoint
Length of Tuple Items before push: 3
Length of Tuple Items after push: 4
Items: Rohit Sharma, 25, JavaTpoint, 10001

Pop()

弹出操作用于从元组中删除元素。

let empTuple = ["Rohit Sharma", 25, "JavaTpoint", 10001];
console.log("Items: "+empTuple);
console.log("Length of Tuple Items before pop: "+empTuple.length);   // returns the tuple size
empTuple.pop();   // removed value to the tuple 
console.log("Length of Tuple Items after pop: "+empTuple.length);
console.log("Items: "+empTuple);

输出:

Items: Rohit Sharma,25, JavaTpoint, 10001
Length of Tuple Items before pop: 4
Length of Tuple Items after pop: 3
Items: Rohit Sharma, 25, JavaTpoint

更新或修改元组元素

元组是可变的,这意味着我们可以更新或更改元组元素的值。要修改元组的字段,我们需要使用字段的索引和赋值运算符。我们可以通过以下示例了解它。

let empTuple = ["Rohit Sharma", 25, "JavaTpoint"];
empTuple[1] = 30;
console.log("Name of the Employee is: "+empTuple [0]);
console.log("Age of the Employee is: "+empTuple [1]);
console.log(empTuple [0]+" is working in "+empTuple [2]);

输出:

Name of the Employee is: Rohit Sharma
Age of the Employee is: 30
Rohit Sharma is working in JavaTpoint

清除元组的字段

我们无法删除元组变量,但可以清除其字段。要清除元组的字段,请为其分配一个空的元组字段集,如以下示例所示。

let empTuple = ["Rohit Sharma", 25, "JavaTpoint"];
empTuple = [];
console.log(empTuple);

输出:

[]

破坏元组

重组使我们能够分解实体的结构。 TypeScript在元组的上下文中使用了解构。

let empTuple = ["Rohit Sharma", 25, "JavaTpoint"];
let [emp, student] = empTuple;
console.log(emp);
console.log(student);

输出:

Rohit Sharma
25

将元组传递给函数

我们可以将元组传递给函数,如下面的示例所示。

//Tuple Declaration
let empTuple = ["JavaTpoint", 101, "Abhishek"];   
//Passing tuples in function  
function display(tuple_values:any[]) {  
   for(let i = 0;i

输出:

JavaTpoint
101
Abhishek