如何在TypeScript中将元素添加到数组的末尾?


在TypeScript中,数组是一种表示元素集合的数据类型。数组中的每个元素在数组中都有一个特定的索引或位置,可以使用它们的索引访问或修改数组中的元素。在TypeScript中,数组可以包含相同或不同数据类型的元素。

要在TypeScript中将元素推送到数组的末尾,可以使用`push()`方法。此方法将元素添加到数组的末尾并返回数组的新长度。

语法

用户可以按照以下语法使用`push()`方法将元素添加到数组的末尾。

let numbers: Array<number> = [10, 40, 32];
numbers.push(90);

参数

Array.push()方法包含一个或多个元素作为参数。

  • 元素 - 它是单个元素或用逗号分隔的多个元素,用于插入到数组的末尾。

示例1

在下面的示例中,我们创建了一个数字数组。此外,我们还使用一些数字初始化了数组。

我们使用Array库的`push()`方法将34插入到数组的末尾。此外,我们在34之后插入了904。在输出中,我们可以看到904是最后一个元素,34是倒数第二个元素。

//  Creating the array of numbers and initialize with some values.
let numbers: Array<number> = [134, 45, 22];
// Push 34 to the end of the array.
numbers.push(34);
console.log("numbers array after inserting 34 at the end is " + numbers);
// Push 904 to the end of the array.
numbers.push(904);
console.log("numbers array after inserting 904 at the end is " + numbers);

编译后,它将生成以下JavaScript代码:

//  Creating the array of numbers and initialize with some values.
var numbers = [134, 45, 22];
// Push 34 to the end of the array.
numbers.push(34);
console.log("numbers array after inserting 34 at the end is " + numbers);
// Push 904 to the end of the array.
numbers.push(904);
console.log("numbers array after inserting 904 at the end is " + numbers);

输出

以上代码将产生以下输出:

numbers array after inserting 34 at the end is 134,45,22,34
numbers array after inserting 904 at the end is 134,45,22,34,904

示例2

在下面的示例中,“strings”是一个包含一些字符串值的字符串数组。在这里,我们传递用逗号分隔的多个字符串作为`push`方法的参数,以便一次性将多个元素插入数组中。`push()`方法按照我们传递字符串作为参数的顺序将元素插入到数组的末尾。

//  Creating the array of strings and initialize with some values.
let strings: Array<string> = ["Welcome", "To", "The"];
// Push multiple elements at the last of the array
strings.push("TutorialsPoint", "!", "Hello", "World!");
console.log(
   "strings array after inserting the multiple elements at the end: " + strings
);

编译后,它将生成以下JavaScript代码:

//  Creating the array of strings and initialize with some values.
var strings = ["Welcome", "To", "The"];
// Push multiple elements at the last of the array
strings.push("TutorialsPoint", "!", "Hello", "World!");
console.log("strings array after inserting the multiple elements at the end: " + strings);

输出

以上代码将产生以下输出:

strings array after inserting the multiple elements at the end: Welcome,To,The,TutorialsPoint,!,Hello,World!

示例3

在下面的示例中,我们创建了Employee接口。该接口包含类变量和方法的声明。之后,我们创建了类型为Employee接口的emp数组,并用Employee的单个对象初始化它。

接下来,我们创建了类型为Employee的new_emp对象,并使用`push()`方法将其插入到emp数组的末尾。我们使用for-of循环打印数组对象。用户可以在输出中看到emp数组包含两个对象,并且new_emp对象插入到最后。

//  Create the inteface for the Employee
interface Employee {
   name: string;
   age: number;
   isPermenent: boolean;
}
//  Create the array of employee
let emp: Employee[] = [{ name: "Shubham", age: 22, is Permenent: true }];
// Create new employee object of type Employee
const new_emp: Employee = {
   name: "Jems",
   age: 30,
   isPermenent: false,
};
//  Push new employee object to emp array
emp.push(new_emp);

console.log("After inserting the new_emp at the end of the emp array is ");
//  Iterate through the emp array and print every employee object
for (let obj of emp) {
   console.log("New Employee!");
   console.log(
      "name: " +
      obj.name +
      " age: " +
      obj.age +
      " isPermenent: " +
      obj.isPermenent
   );
}

编译后,它将生成以下JavaScript代码:

//  Create the array of employee
var emp = [{ name: "Shubham", age: 22, is Permenent: true }];
// Create new employee object of type Employee
var new_emp = {
   name: "Jems",
   age: 30,
   isPermenent: false
};
//  Push new employee object to emp array
emp.push(new_emp);
console.log("After inserting the new_emp at the end of the emp array is ");
//  Iterate through the emp array and print every employee object
for (var _i = 0, emp_1 = emp; _i < emp_1.length; _i++) {
   var obj = emp_1[_i];
   console.log("New Employee!");
   console.log("name: " +
      obj.name +
      " age: " +
      obj.age +
      " isPermenent: " +
      obj.isPermenent);
}

输出

以上代码将产生以下输出:

After inserting the new_emp at the end of the emp array is 
New Employee!
name: Shubham age: 22 is Permenent: true
New Employee!
name: Jems age: 30 is Permenent: false

我们学习了如何在数组末尾插入单个或多个元素。在最后一个示例中,我们还学习了如何使用`push()`方法在数组末尾插入对象。

更新于:2022年12月19日

3K+ 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告