Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Add findLastIndex function to std/collections #1060

Closed
kawarimidoll opened this issue Jul 24, 2021 · 1 comment · Fixed by #1062
Closed

[Feature Request] Add findLastIndex function to std/collections #1060

kawarimidoll opened this issue Jul 24, 2021 · 1 comment · Fixed by #1062

Comments

@kawarimidoll
Copy link
Contributor

kawarimidoll commented Jul 24, 2021

Array has finding functions, find() and findIndex().
find() returns the element, and findIndex() returns the index.

Now, we have findLast() in std/collections, this returns the element.

So, how about add findLastIndex() ?
It is almost same as findLast(), but returns the index of the last found element.

import { Predicate } from "./types.ts";

export function findLastIndex<T>(
  array: Array<T>,
  predicate: Predicate<T>,
): number {
  for (let i = array.length - 1; i >= 0; i -= 1) {
    const element = array[i];

    if (predicate(element)) {
      return i;
    }
  }

  return -1;
}
@kt3k
Copy link
Member

kt3k commented Jul 26, 2021

Because we already have findLast in std/collections, this proposal makes sense to me.

This can be implemented in V8 in some future https://github.com/tc39/proposal-array-find-from-last https://chromium-review.googlesource.com/c/v8/v8/+/3037160 . If that happens, then let's deprecate it.

@kt3k kt3k mentioned this issue Jul 27, 2021
26 tasks
@LionC LionC mentioned this issue Aug 27, 2021
44 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants