如何在 TypeScript 中查詢陣列中元素的最後一次出現?
我們將學習如何在 TypeScript 中找到陣列中元素的最後一個索引。在開發過程中,資料陣列可能包含重複資料,我們可能需要保留元素的最後一次出現。
例如,我們從資料庫中獲取了所有使用者的登入歷史記錄。現在,我們想找到特定使用者最後一次登入的時間。在這種情況下,我們可以使用以下方法來查詢陣列中元素的最後一次出現。
從陣列的末尾開始搜尋
為了找到元素的最後一次出現,我們可以從末尾開始搜尋該元素。當我們從末尾找到該元素的第一次出現時,我們可以將該索引保留為從開頭算起的該元素的最後一次出現。
語法
使用者可以按照以下語法在 TypeScript 中搜索陣列中元素的最後一次出現。
let simple_arr: Array<number> = [
20, 232, 3, 21, 23, 343, 4, 4, 3, 3, 20, 20, 54, 43,
];
let element_to_search: number = 20;
for (let i = simple_arr.length - 1; i >= 0; i--) {
if (simple_arr[i] == element_to_search) {
return i;
// last index of element is i;
}
}
演算法
步驟 1 − 定義任何資料型別的陣列。
步驟 2 − 使用 for 迴圈從末尾迭代陣列。
步驟 3 − 將變數“i”初始化為陣列的長度-1,並迭代直到“i”小於零。
步驟 4 − 在 for 迴圈內,檢查我們是否找到了搜尋元素的第一個匹配項,並返回當前索引 i。
步驟 5 − 如果在陣列的完整迭代後我們沒有找到搜尋元素,則返回 -1。
示例
在下面的示例中,我們建立了一個名為 searchFromLast() 的函式,該函式將搜尋元素作為引數並返回搜尋元素的最後一個索引。
為了找到陣列中元素的最後一次出現,我們在 searchFromLast() 函式中實現了上述演算法。
// Creating the numbers array
let simple_arr: Array<number> = [
20, 232, 3, 21, 23, 343, 4, 4, 3, 3, 20, 20, 54, 43,
];
// function to find the last index of element
function searchFromLast(element: number): number {
// use the for loop to start searching from the last
for (let i = simple_arr.length - 1; i >= 0; i--) {
// return the first occurence of the element from the last
if (simple_arr[i] == element) {
return i;
}
}
// if element not found, return -1
return -1;
}
// call the searchFromLast() function for various elements
console.log(
"The last occurence of the 20 in the array is at index " + searchFromLast(20)
);
console.log(
"The last occurence of the 3 in the array is at index " + searchFromLast(3)
);
console.log(
"The last occurence of the -3 in the array is at index " + searchFromLast(-3)
);
編譯後,它將生成以下 JavaScript 程式碼
// Creating the numbers array
var simple_arr = [
20, 232, 3, 21, 23, 343, 4, 4, 3, 3, 20, 20, 54, 43,
];
// function to find the last index of element
function searchFromLast(element) {
// use the for loop to start searching from the last
for (var i = simple_arr.length - 1; i >= 0; i--) {
// return the first occurence of the element from the last
if (simple_arr[i] == element) {
return i;
}
}
// if element not found, return -1
return -1;
}
// call the searchFromLast() function for various elements
console.log("The last occurence of the 20 in the array is at index " + searchFromLast(20));
console.log("The last occurence of the 3 in the array is at index " + searchFromLast(3));
console.log("The last occurence of the -3 in the array is at index " + searchFromLast(-3));
輸出
以上程式碼將產生以下輸出:
The last occurence of the 20 in the array is at index 11 The last occurence of the 3 in the array is at index 9 The last occurence of the -3 in the array is at index -1
使用 TypeScript 的 findLastIndex() 方法
在 TypeScript 中,findLastIndex() 方法是內建庫方法。我們可以使用它來查詢陣列中特定元素的最後一次出現。它將搜尋元素作為引數並返回其最後一個索引。
語法
使用者可以按照以下語法使用 findLastIndex() 方法在 TypeScript 中搜索元素的最後一次出現。
reference_arr.lastIndexOf(element);
引數
reference_arr − 這是我們要在其中搜索特定元素的陣列。
element − 這是我們要查詢其最後一次出現的搜尋元素。
返回值
如果元素存在於陣列中,則返回搜尋元素的基於零的最後一個索引;否則返回 -1。
示例
在下面的示例中,我們使用了 lastIndexOf() 方法以及陣列來查詢各種字串的最後一次出現。我們在下面的示例中建立了一個包含重複元素的字串陣列,然後搜尋了不同的元素。
// array containing the different values
let string_arr: Array<string> = [
"Hi!",
"Hello",
"Hi!",
"Hello",
"Hi!",
"Hello",
"TutorialsPoint",
"Hello",
];
// using the lastIndexOf() method with the array to search for particular elements
console.log(
"The last index of the hello in the array is " +
string_arr.lastIndexOf("Hello")
);
console.log(
"The last index of the Hi! in the array is " + string_arr.lastIndexOf("Hi!")
);
console.log(
"The last index of the user in the array is " + string_arr.lastIndexOf("user")
);
編譯後,它將生成以下 JavaScript 程式碼
// array containing the different values
var string_arr = [
"Hi!",
"Hello",
"Hi!",
"Hello",
"Hi!",
"Hello",
"TutorialsPoint",
"Hello",
];
// using the lastIndexOf() method with the array to search for particular elements
console.log("The last index of the hello in the array is " +
string_arr.lastIndexOf("Hello"));
console.log("The last index of the Hi! in the array is " + string_arr.lastIndexOf("Hi!"));
console.log("The last index of the user in the array is " + string_arr.lastIndexOf("user"));
輸出
以上程式碼將產生以下輸出:
The last index of the hello in the array is 7 The last index of the Hi! in the array is 4 The last index of the user in the array is -1
我們學習了自定義演算法來查詢陣列中元素的最後一次出現。自定義演算法僅用於學習目的,使用者可以使用 lastIndexOf() 方法來獲得相同的輸出。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP