如何在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+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告