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

Array.prototype.findLastIndex() #1253

Closed
teriyakigod2 opened this Issue Jul 4, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@teriyakigod2

teriyakigod2 commented Jul 4, 2018

indexOf() - lastIndexOf()
findIndex() -  ????

Why do we have findIndex method, but no findLastIndex? It seems logical to follow[index|lastIndex]Of` pair, doesn't it? Reversing array to find the last element satisfies the condition seems too complicated to me.

Example:

function isPrime(element, index, array) {
  var start = 2;
  while (start <= Math.sqrt(element)) {
    if (element % start++ < 1) {
      return false;
    }
  }
  return element > 1;
}

console.log([4, 6, 8, 12].findIndex(isPrime)); // -1, not found
console.log([7, 4, 6, 7, 12].findIndexLast(isPrime)); // 3

Would be glad to implement, if it makes sense.

Thx for replies.

@WebReflection

This comment has been minimized.

Show comment
Hide comment
@WebReflection

WebReflection Jul 4, 2018

FWIW, indexOf => lastIndexOf so that findIndex => findLastIndex is the only name that makes sense to me.

However, reduce => reduceRight is also a thing so maybe findIndexRight would make sense too?

That leaves room for forEachRight, someRight, everyRight as well, so maybe it's a bit too much to ask :-)

WebReflection commented Jul 4, 2018

FWIW, indexOf => lastIndexOf so that findIndex => findLastIndex is the only name that makes sense to me.

However, reduce => reduceRight is also a thing so maybe findIndexRight would make sense too?

That leaves room for forEachRight, someRight, everyRight as well, so maybe it's a bit too much to ask :-)

@ljharb

This comment has been minimized.

Show comment
Hide comment
@ljharb

ljharb Jul 4, 2018

Member

Maybe somebody can speak to any motivation for not adding it.

However, please see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md for how to best suggest new features for the language.

Member

ljharb commented Jul 4, 2018

Maybe somebody can speak to any motivation for not adding it.

However, please see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md for how to best suggest new features for the language.

@ljharb ljharb closed this Jul 4, 2018

@teriyakigod2

This comment has been minimized.

Show comment
Hide comment
@teriyakigod2

teriyakigod2 Jul 5, 2018

@ljharb Thx, my fault i didn't read it out before.

teriyakigod2 commented Jul 5, 2018

@ljharb Thx, my fault i didn't read it out before.

@teriyakigod2 teriyakigod2 changed the title from Array.prototype.findIndexLast() to Array.prototype.findLastIndex() Jul 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment