TypeScript 元组
正如我们所知,数组由同质(相同)类型的值组成,但有时当我们需要将不同类型值的集合存储在单个变量中时,我们将使用元组。它们就像 C 编程中的结构一样,也可以作为函数调用中的参数传递。元组可能是一种或多种类型的数据(如数字与字符串或字符串与数字等)。
- 为了表示多维坐标系,使用的术语是抽象数学中的元组。
- 在 JavaScript 中,我们没有元组作为数据类型,但在 typescript 中,元组工具是可用的。
句法
let tuple_name = [val1, val2, val3, ...val n];
例子:
let arrTuple = [501, "welcome", 505, "Mohan"];
console.log(arrTuple);
输出:
[501, ‘welcome’, 105, ‘Mohan’]
通过最初在 Typescript 中将元组声明为空元组来分别声明和初始化元组。例子:
let arrTuple = [];
arrTuple[0] = 501
arrTuple[1] = 506
访问元组元素借助索引基础,我们可以读取或访问元组的字段,这与数组相同。索引也从零开始。
例子:
var employee: [number, string] = [1, "Steve"];
employee[0]; // returns 1
employee[1]; / return Steve
输出:
1 Steve
我们可以同时在元组中声明异构数据类型,例如:数字和字符串。
例子
let empTuple = ["Vivek Singh", 22, "Honesty"];
console.log("Name of the Employee is : "+empTuple [0]);
console.log("Age of the Employee is : "+empTuple [1]);
console.log(empTuple [0]+" is workinging in "+empTuple [2]);
输出:
Name of the Employee is : Vivek Singh Age of the Employee is : 22 Vivek Singh is workinging in Microsoft
元组上的操作一个元组有两个操作:
- 推()
- 流行音乐()
Push()通过 push 操作向元组中添加一个元素。例子
var employee: [number, string] = [1, "Steve"];
employee.push(2, "Bill");
console.log(employee);
输出:
[1, ‘Steve’, 2, ‘Bill’]
这种类型的声明在元组中是允许的,因为我们正在向元组添加数字和字符串值,并且它们对员工元组有效。
例子
let empTuple = ["Vivek Singh", 22, "Honesty"];
console.log("Items: "+empTuple); // here we print tuple elements
empTuple.push(10001); // append value to the tuple
console.log("Length of Tuple Items after push: "+empTuple.length); // After pushing elements in tuples calculate length of tuples.
console.log("Items: "+empTuple);
输出:
Items: Vivek Singh, 22, Honesty Length of Tuple Items after push: 4 Items: Vivek Singh, 22, Honesty, 10001
使用推送操作将元素添加到元组。
例子
let empTuple = ["Mohit Singh", 25, "geeksforgeeks", 10001];
console.log("Items: "+empTuple); // here we print tuple elements
empTuple.pop(); // removed value to the tuple
console.log("Length of Tuple Items after pop: "+empTuple.length); After pushing elements in tuples calculate length of tuples.
console.log("Items: "+empTuple);
输出:
Items: Mohit Singh, 25, geeksforgeeks, 10001 Length of Tuple Items after pop: 3 Items: Mohit Singh, 25, geeksforgeeks
更新或修改元组元素我们需要使用字段的索引和赋值运算符来修改元组的字段。它可以在以下示例中显示。例子
let empTuple = ["Ganesh Singh", 25, "TCS"];
empTuple[1] = 60;
console.log("Name of the Employee is: "+empTuple [0]);
console.log("Age of the Employee is: "+empTuple [1]);
console.log(empTuple [0]+" is workinging in "+empTuple [2]);
输出:
Name of the Employee is: Ganesh Singh Age of the Employee is: 60 Ganesh Singh is workinging in TCS
清除元组字段可以清除但我们不能删除元组变量。要清除元组的字段,请为其分配一个空的元组字段集,如下所示:
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
下面显示的代码片段将帮助我们理解在 TypeScript 中创建元组:-
例子:
Javascript
let student_details : [number, string, string] = [1 , "Aman" , "CSE"];
student_details.push(2 , "Ram" , "CSE");
console.log(student_details);
// This code is contributed by Aman Singla...
输出:
[ 1, 'Aman', 'CSE', 2, 'Ram', 'CSE' ]