Nocturnal Pumpkin Maelstrom
Miss any of our Open RFC calls?Watch the recordings here! »

find-last-index-x

3.1.2 • Public • Published

Travis status Dependency status devDependency status npm version jsDelivr hits bettercodehub score Coverage Status

find-last-index-x

Search an array from the end and return the matched index.

module.exports(array, callback, [thisArg])number

Like findIndex, this method returns an index in the array, if an element in the array satisfies the provided testing function, except it is peformed in reverse. Otherwise -1 is returned.

Kind: Exported function
Returns: number - Returns index of positively tested element, otherwise -1.
Throws:

  • TypeError If array is null or undefined-
  • TypeError If callback is not a function.
Param Type Description
array Array The array to search.
callback function Function to execute on each value in the array, taking three arguments: element, index and array.
[thisArg] * Object to use as this when executing callback.

Example

import findLastIndex from 'find-index-x';
 
function isPrime(element, index, array) {
  var start = 2;
  while (start <= Math.sqrt(element)) {
    if (element % start++ < 1) {
      return false;
    }
  }
  return element > 1;
}
 
console.log(findLastIndex([4, 6, 8, 12, 14], isPrime)); // -1, not found
console.log(findLastIndex([4, 6, 7, 12, 13], isPrime)); // 4

Install

npm i find-last-index-x

DownloadsWeekly Downloads

9

Version

3.1.2

License

MIT

Unpacked Size

259 kB

Total Files

11

Last publish

Collaborators

  • avatar