如何在 TypeScript 中查找数组中元素的最后一次出现?

typescriptweb developmentfront end technology

我们将学习如何在 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;
      // 元素的最后一个索引是 i;
   }
}

算法

  • 步骤 1 - 定义任意数据类型的数组。

  • 步骤 2 - 使用 for 循环从最后一个元素开始迭代数组。

  • 步骤 3 - 将变量 i 初始化为数组长度 -1,并迭代直到 i 小于零。

  • 步骤 4 - 在 for 循环中,检查是否找到了搜索元素的第一个匹配项,并返回当前索引 i。

  • 步骤 5 - 如果在数组完整迭代后仍未找到搜索元素,则返回 -1。

示例

在下面的示例中,我们创建了名为searchFromLast(),它将搜索元素作为参数,并返回搜索元素的最后一个索引。

为了查找元素在数组中最后一次出现的位置,我们在 searchFromLast() 函数中实现了上述算法。

// 创建数字数组
let simple_arr: Array<number> = [
   20, 232, 3, 21, 23, 343, 4, 4, 3, 3, 20, 20, 54, 43,
];

// 用于查找元素最后一个索引的函数
function searchFromLast(element: number): number {

    // 使用 for 循环从最后一个元素开始搜索
    for (let i = simple_arr.length - 1; i >= 0; i--) {
    
        // 从最后一个元素开始返回该元素的第一次出现
        if (simple_arr[i] == element) {
        return i;
    }
    }
    // 如果未找到元素,则返回 -1
    return -1;
}
// 对不同的元素调用 searchFromLast() 函数
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)
);

On compiling, it will generate the following JavaScript code:


// 创建数字数组
var simple_arr = [
    20, 232, 3, 21, 23, 343, 4, 4, 3, 3, 20, 20, 54, 43,
];
// 查找元素最后一个索引的函数
function searchFromLast(element) {
   // 使用 for 循环从最后一个元素开始搜索
   for (var i = simple_arr.length - 1; i >= 0; i--) {
      // 返回元素从最后一个元素开始第一次出现的位置
      if (simple_arr[i] == element) {
         return i;
      }
   }
   // 如果未找到元素,则返回 -1
   return -1;
}
// 针对各个元素调用 searchFromLast() 函数
console.log("数组中 20 的最后一次出现位置为索引 " + searchFromLast(20));
console.log("数组中 3 的最后一次出现位置为索引 " + searchFromLast(3));
console.log("数组中 -3 的最后一次出现位置为索引 " + searchFromLast(-3));

输出

上述代码将产生以下输出 -

数组中 20 最后一次出现在索引 11 处
数组中 3 最后一次出现在索引 9 处
数组中 -3 最后一次出现在索引 -1 处

使用 TypeScript 的 findLastIndex() 方法

在 TypeScript 中,findLastIndex() 方法是内置库方法。我们可以使用它来查找数组中特定元素的最后一次出现。它以搜索元素作为参数,并返回其最后一个索引。

语法

用户可以按照以下语法使用 findLastIndex() 方法在 TypeScript 中搜索元素的最后一次出现。

reference_arr.lastIndexOf(element);

参数

  • reference_arr − 我们要在其中搜索特定元素的数组。

  • element − 我们需要查找其最后一次出现的搜索元素。

返回值

如果元素存在于数组中,则返回搜索元素从零开始的最后一个索引;否则,返回 -1。

示例

在下面的示例中,我们使用 lastIndexOf() 方法对数组进行操作,查找不同字符串的最后一次出现。我们创建了包含重复元素的字符串数组,然后在下面的示例中搜索了不同的元素。

// 包含不同值的数组
let string_arr: Array<string> = [
   "Hi!",
   "Hello",
   "Hi!",
   "Hello",
   "Hi!",
   "Hello",
   "TutorialsPoint",
   "Hello",
];
// 使用数组的 lastIndexOf() 方法搜索特定元素
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 代码:


// 包含不同值的数组
var string_arr = [
   "Hi!",
   "Hello",
   "Hi!",
   "Hello",
   "Hi!",
   "Hello",
   "TutorialsPoint",
   "Hello",
];
// 使用数组的 lastIndexOf() 方法搜索特定元素
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() 方法实现相同的输出。


相关文章