如何在 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